0% found this document useful (0 votes)
4 views30 pages

C_Class_Exercise.pptx

C++ presentation for student

Uploaded by

Vinh NgTr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views30 pages

C_Class_Exercise.pptx

C++ presentation for student

Uploaded by

Vinh NgTr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

C++ Inheritance and

Polymorphism
Introduction to Computer Science – C++ Exercise

Public, Protected and Private Inheritance


▪ public inheritance makes public members of the base
class public in the derived class, and the protected
members of the base class remain protected in the
derived class

▪ protected inheritance makes the public and protected


members of the base class protected in the derived class

▪ private inheritance makes the public and protected


members of the base class private in the derived class

▪ private members of the base class are inaccessible to


the derived class
2
Introduction to Computer Science – C++ Exercise

Example 1 // Error: member "Base::pvt" is inaccessible


cout << "Private = " << object1.pvt;

// Error: member "Base::prot" is inaccessible


cout << "Protected = " << object1.prot;

3
Introduction to Computer Science – C++ Exercise

Example 2

4
Introduction to Computer Science – C++ Exercise

Example 3

// Error: member "Base::getPVT()" is


inaccessible
cout << "Private = " << object1.getPVT();

// Error: member "Base::pub" is inaccessible


cout << "Public = " << object1.pub;

5
Introduction to Computer Science – C++ Exercise

Inheritance
class Vehicle {
public:
string brand = “HONDA";
void func() {
cout << “I am HONDA! \n" ;
}
};

class Car: public Vehicle {


public:
string model = “CIVIC";
};
int main() {
Car myCar;
myCar.func();
cout << myCar.brand + " " + myCar.model;
return 0;
}

6
Introduction to Computer Science – C++ Exercise

Multi Level Inheritance


class MyClass {
public:
void myFunction() {
cout << "Some content in parent class." ;
}
};

class MyChild: public MyClass {


};

class MyGrandChild: public MyChild {


};

int main() {
MyGrandChild myObj;
myObj.myFunction();
return 0;
}

7
Introduction to Computer Science – C++ Exercise

Multiple class MyClass {


public:
Inheritance void myFunction() {
cout << "Some content in parent class." ;
}
};

class MyOtherClass {
public:
void myOtherFunction() {
cout << "Some content in another class." ;
}
};

class MyChildClass: public MyClass, public MyOtherClass {


};

int main() {
MyChildClass myObj;
myObj.myFunction();
myObj.myOtherFunction();
return 0;
} 8
Introduction to Computer Science – C++ Exercise

Access Specified class Employee {


protected:
int salary;
};

// Derived class
Public: members of a class class Programmer: public Employee {
public:
are accessible from outside int bonus;
the class void setSalary(int s) {
salary = s;
}
Private: members can only int getSalary() {
be accessed within the class) return salary;
}
Protected: is similar to };
private, but it can also be int main() {
accessed in the inherited Programmer myObj;
class: myObj.setSalary(50000);
myObj.bonus = 15000;
cout << "Salary: " << myObj.getSalary() << "\n";
cout << "Bonus: " << myObj.bonus << "\n";
return 0;
}

9
Introduction to Computer Science – C++ Exercise

Polymorphism
// Base class
class Animal {
public:
void animalSound() {
cout << "The animal makes a sound \n";
} int main() {
}; Animal myAnimal;
// Derived class Pig myPig;
class Pig : public Animal { Dog myDog;
public:
void animalSound() {
cout << "The pig says: wee wee \n"; myAnimal.animalSound();
} myPig.animalSound();
};
myDog.animalSound();
// Derived class return 0;
class Dog : public Animal {
public: }
void animalSound() {
cout << "The dog says: bow wow \n";
}
};

10
C++ Inheritance and
Polymorphism MCQs
Introduction to Computer Science – C++ Exercise

MCQ

1. What is Inheritance in C++?


a) Wrapping of data into a single class
b) Deriving new classes from existing
classes
c) Overloading of classes
d) Classes with same names

12
Introduction to Computer Science – C++ Exercise

MCQ

What is meant by multiple inheritance?

A. Deriving a base class from derived class


B. Deriving a derived class from base class
C. Deriving a derived class from more
than one base class
D. None of the mentioned

13
Introduction to Computer Science – C++ Exercise

MCQ

2. How many specifies are used to derive


a class?
a) 1
b) 2
c) 3
d) 4

14
Introduction to Computer Science – C++ Exercise

MCQ

3. Which specifier makes all the data


members and functions of base class
inaccessible by the derived class?
a) private
b) protected
c) public
d) both private and protected

15
Introduction to Computer Science – C++ Exercise

MCQ

When the inheritance is private, the private


methods in base class are __________ in the
derived class (in C++).

A. Inaccessible
B. Accessible
C. Protected
D. Public

16
Introduction to Computer Science – C++ Exercise

MCQ

4. If a class is derived privately from a base class


then
a) no members of the base class is inherited
b) all members are accessible by the derived class
c) all the members are inherited by the class but
are hidden and cannot be accessible
d) no derivation of the class gives an error

17
Introduction to Computer Science – C++ Exercise

MCQ

What will be the order of execution of base


class constructors in the following method
of inheritance.class a: public b, public c {...};

A. b(); c(); a();


B. c(); b(); a();
C. a(); b(); c();
D. b(); a(); c();

18
Introduction to Computer Science – C++ Exercise

MCQ

Inheritance allow in C++ Program?

A. Class Re-usability
B. Creating a hierarchy of classes
C. Extendibility
D. All of the above

19
Introduction to Computer Science – C++ Exercise

What is a virtual function in C++?


a) Any member function of a class
b) All functions that are derived from the base class
c) All the members that are accessing base class data members
d) All the functions which are declared in the base class and
is re-defined/overridden by the derived class

20
Introduction to Computer Science – C++ Exercise

What will be the output of the following C++ code?

a) 1010
b) 1510
c) 1515
d) 5110

21
Introduction to Computer Science – C++ Exercise

What will be the output of the following C++ code?

22
Introduction to Computer Science – C++ Exercise

What will be the output of the following C++ code?

a) Hello this is class B


b) Hello this is class A
c) Error
d) Segmentation fault

23
Introduction to Computer Science – C++ Exercise

What will be the output of the following C++ code?

a) Hello this is class A


b) Hello this is class B
c) Error
d) Segmentation Fault

24
Introduction to Computer Science – C++ Exercise

What will be the output of the following C++ code?

a) Hello this is class A


b) Hello this is class B
c) Error
d) Segmentation Fault

25
Introduction to Computer Science – C++ Exercise

What will be the output of the following C++ code?

A. Compiler Dependent
B. Base1 Base2 Derived
C. Base2 Base1 Derived
D. Compiler Error

26
Introduction to Computer Science – C++ Exercise

What will be the output of the following C++ code?

A. Base1 Base2 Derived


B. Derived Base2 Base1
C. Derived
D. Compiler Dependent

27
Introduction to Computer Science – C++ Exercise

What will be the output of the following C++ code?

A. error: invalid conversion from "Derived*"


to "Base*"
B. No Compiler Error
C. error: invalid conversion from "Base*"
to "Derived*"
D. Runtime Error

28
Introduction to Computer Science – C++ Exercise

What will be the output of the following C++ code?

A. Base::lfc(int i) called
B. Derived::lfc() called
C. Base::lfc() called
D. Compiler Error
29
Introduction to Computer Science – C++ Exercise

What will be the output of the following C++ code?

A. In find
B. In course
C. Compiler Error: Ambiguous call to
print()
D. None of the above
30

You might also like