--Your friends at LectureNotes

Note for Object Oriented Programming Using Cpp - OOP by nallamalli sushma

  • Object Oriented Programming Using Cpp - OOP
  • Note
  • Computer Science Engineering
  • B.Tech
  • 7 Topics
  • 7 Offline Downloads
  • Uploaded 1 year ago
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-2

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

Text from page-3

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

Text from page-4

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

Text from page-5

OBJECT-ORIENTED PROGRAMMING THROUGH C++ This kind of relationship is known as has-a-relationship or containership. class A { public: int x; A() { x=20; cout<<"\n In A constructor"; } }; class B { public: int y; A a; B() { y=30; cout<<"\n In B constructor"; } void show() { cout<<"\n X="<<a.x<<"\t Y="<<y; } }; int main() { B b; b.show(); return 0; } Output In A constructor In B constructor X=20 Y=30 Define abstract class. What is the use of abstract classes? Explain.  When a class is not used for creating objects , it is called as abstract class. DMSSVH COLLEGE OF ENGINEERING 68

Lecture Notes