12 constructors invocation and data members initialization

490 views

Published on

constructors invocation and data members initialization

Published in: Sports, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
490
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
24
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

12 constructors invocation and data members initialization

  1. 1. 09/04/131 VIT - SCSE • In multiple inheritance, the constructors of base classes are invoked first, in order in which they appear in the declaration of the derived class. • In multilevel inheritance, they are executed in the order of inheritance. • Data member initialization is represented by • Datamembername(value) • The value can be arguments of a constructor, expression or other data members. Constructors Invocation and Data Members Initialization
  2. 2. 09/04/132 VIT - SCSE class B { protected: int x,y; public: B(int a,int b):x(a),y(b) //x=a, y=b { } }; class D:public B { private: int a,b; public: D(int p,int q,int r):a(p),B(p,q),b(r) { } void output() { cout<<”x=”<<x<<endl; cout<<”y=”<<y<<endl; cout<<”a=”<<a<<endl; cout<<”b=”<<b<<endl; } }; void main() { D ob(5,10,15); ob.output(); }
  3. 3. 09/04/133 VIT - SCSE The following examples illustrate the initialization of data members with different formats: B(int a,int b):x(a),y(a+b) B(int a,int b):x(a),y(x+b) B(int a,int b):y(a),x(y+b)
  4. 4. 09/04/134 VIT - SCSE Overloaded member functions If the same member (data/function) exists in both the base class and the derived class, the member in the derived class will be executed. Abstract class Class with no object Use for inheritance only Can derive classes from an abstract class
  5. 5. 09/04/135 VIT - SCSE class baseA { protected: int x; ….. …. }; class derivedB:public baseA { protected: ….. …. }; class derivedD:public baseA { protected: ….. …. }; class abc:public derivedB,public derived { …… …… }; Virtual Base Class  Multipath inheritance
  6. 6. 09/04/136 VIT - SCSE • The data member x is inherited twice in the derived class abc, once through the derived class derivedB and again through derivedD. • This is wasteful. • The above multiple repetition of the data member can be corrected by changing the derived class derivedB and derivedD into virtual base classes. • Any base class which is declared using the keyword  virtual is called a virtual base class.
  7. 7. 09/04/137 VIT - SCSE class baseA { protected: int x; ….. …. }; class derivedB:public virtual baseA { protected: ….. …. }; class derivedD:public virtual baseA { protected: ….. …. }; class abc:public derivedB,public derivedD { …… …… };
  8. 8. 09/04/138 VIT - SCSE By making derivedB and derivedD into virtual base classes for abc, a copy of the data member x is available only once.
  9. 9. 09/04/139 VIT - SCSE class A { protected: int x; public: void getdata(); void display(); }; class B:public virtual A { protected: float y; public: void getdata(); void display(); }; class C:public virtual A { protected: char name[20]; public: void getdata(); void display(); }; class D:public B,public C { public: void getdata(); void display(); };
  10. 10. 09/04/1310 VIT - SCSE void A:getdata() { cout<<”Enter an integer”; cin>>x; } void A:display() { cout<<”Integer:”<<x<,endl; } void B:getdata() { A::getdata(); cout<<”Enter a floating point value”; cin>>y; } void B:display() { A::display(); cout<<”Real Number:”<<y<<endl; } void C:getdata() { A::getdata(); cout<<”Enter a string”; cin>>name; } void C:display() { A::display(); cout<<”String:”<<name< <endl; } void D:getdata() { B::getdata(); C::getdata(); }
  11. 11. 09/04/1311 VIT - SCSE void D:display() { B::display(); C::display(); } void main() { D ob; ob.getdata(); ob.display(); }

×