×
Nothing in the world is more common than unsuccessful people with talent.
--Your friends at LectureNotes
Close

Object Oriented Programming Using Cpp

by Nallamalli Sushma
Type: NoteCourse: B.Tech Specialization: Computer Science EngineeringOffline Downloads: 3Views: 24Uploaded: 15 days agoAdd to Favourite

Touch here to read
Page-1

Object Oriented Programming Using Cpp by Nallamalli Sushma

Topic:
Nallamalli Sushma
Nallamalli Sushma

/ 172

Share it with your friends

Suggested Materials

Leave your Comments

Contributors

Nallamalli Sushma
Nallamalli Sushma
OBJECT-ORIENTED PROGRAMMING THROUGH C++ Multi-path Inheritance In this, one class is derived from two base classes and in turn these two classes are derived from a single base class in known as Multi-path Inheritance. class A { //class A definition }; class B: public A { //class B definition }; class C: public A { //class C definition }; class D :public B, public C { //class D definition }; A is a base class. The classes B and C are derived from base class A. Both B and C inherit properties of class A. Further, class D is derived from B and C.B and C have same copies of members inherited from A. The same are inherited to D. Here, ambiguity is generated. Hence, virtual keyword is used to avoid ambiguity. Explain about virtual base classes with an example. (OR) How can you overcome the ambiguity occurring due to multipath inheritance? Explain with an example. To overcome the ambiguity due to multipath inheritance the keyword virtual is used. When classes are derived as virtual, the compiler takes essential caution to avoid the duplication of members. So, we make a class virtual if it is a base class that has been used by more than one derived class as their base class. DMSSVH COLLEGE OF ENGINEERING 64
OBJECT-ORIENTED PROGRAMMING THROUGH C++ Uses: When two or more classes are derived from a common base class, we can prevent multiple copies of the base class in the derived classes by using virtual keyword. This can be achieved by preceding the keyword “virtual” to the base class. Example class A { protected: int a1; }; class B: public virtual A { protected: int a2; }; class C: public virtual A { protected: int a3; }; class D :public B, public C { int a4; public: void input() { cout<<”Enter a1,a2,a3 and a4 values:”; cin>> a1>>a2>>a3>>a4; } void show() { cout<<”a1=”<<a1<<”\na2=”<<a2; cout<<”\na3=”<<a3<<”\na4=”<<a4; } }; int main() { D d; d.input(); d.show(); return 0; DMSSVH COLLEGE OF ENGINEERING 65
OBJECT-ORIENTED PROGRAMMING THROUGH C++ } Output Enter a1, a2, a3 and a4 values: 10 20 30 40 a1=10 a2=20 a3=30 a4=40 How constructors and destructors are executed in inherited class? Explain with an example. The constructors are used to initialize the member variables and the destructors are used to destroy the object. The compiler automatically invokes the constructor and destructors. Rules:  The derived class does not require a constructor, if the base class contains default constructor (zero-argument constructor).  If the base class is having a parameterized constructor, then it is necessary to declare a constructor in derived class also. The derived class constructor passes arguments to the base class constructor.  In inheritance, normally derived classes are used to declare objects. So, it is necessary to define constructors in the derived class.  When an object of a derived class is declared, the constructors of the base and derived classes are executed. class A { public: A() { cout<<"\n Class A constructor called"; } ~A() { cout<<"\nClass A destructor called"; } }; class B : public A { public: B() { cout<<"\n Class B constructor called"; DMSSVH COLLEGE OF ENGINEERING 66
OBJECT-ORIENTED PROGRAMMING THROUGH C++ } ~B() { cout<<"\nClass B destructor called"; } }; class C : public B { public: C() { cout<<"\n Class C constructor called"; } ~C() { cout<<"\nClass C destructor called"; } }; int main() { C c; 6yreturn 0; } Output Class A constructor called Class B constructor called Class C constructor called Class C destructor called Class B destructor called Class A destructor called How can you pass an object as a class member? Explain. (OR) Explain about delegation with an example. (OR) Explain about container classes with an example. Properties of one class can be used in another class using inheritance or using the object of a class as a member in another class. Declaring the object as a class data member in another class is known as delegation. When a class has an object of another class as its member, such a class is known as a container class. DMSSVH COLLEGE OF ENGINEERING 67

Lecture Notes