SlideShare a Scribd company logo
1 of 41
Download to read offline
Prepared by
Mohammed Sikander
Technical Lead
Cranes Software
mohammed.sikander@cranessoftware.com
class Base{
private : int bpriv;
protected : int bprot;
Public : int bpub;
};
class Derived : public Base{
};
Sikander 2
int main( )
{
cout << sizeof(Derived) << endl;
}
class Base
{
};
class Derived : public
Base
{
};
Sikander 3
class Base
{
int b;
};
class Derived : public Base
{
};
class Base {
};
class Derived : public Base {
int d;
};
class Base
{
private : int privBase;
protected : int protBase;
public : int pubBase;
};
class Derived : public Base
{
public : void derFunc( )
{
1. privBase = 5;
2. protBase = 10;
3. pubBase = 15;
}
};
Sikander 4
int main( )
{
Derived dobj;
4. dobj.privBase = 5;
5. dobj.protBase = 10;
6. dobj.pubBase = 15;
}
class Base
{
private : int privBase;
protected : int protBase;
public : int pubBase;
};
class Derived : protected Base
{
public : void derFunc( )
{
1. privBase = 5;
2. protBase = 10;
3. pubBase = 15;
}
};
Sikander 5
int main( )
{
Derived dobj;
4. dobj.privBase = 5;
5. dobj.protBase = 10;
6. dobj.pubBase = 15;
}
class Base
{
private : int privBase;
protected : int protBase;
public : int pubBase;
};
class Derived : private Base
{
public : void derFunc( )
{
1. privBase = 5;
2. protBase = 10;
3. pubBase = 15;
}
};
Sikander 6
int main( )
{
Derived dobj;
4. dobj.privBase = 5;
5. dobj.protBase = 10;
6. dobj.pubBase = 15;
}
class Base {
private : int privBase;
protected : int protBase;
public : int pubBase;
};
class Derived : private Base{
public : void derivedFunc( ) {
1. privBase = 5;
2. protBase = 10;
3. pubBase = 15;
}
};
class Der : public Derived {
public : void derFunc( ) {
4. privBase = 5;
5. protBase = 10;
6. pubBase = 15;
}
};
Sikander 7
class Base {
private : int privBase;
protected : int protBase;
public : int pubBase;
};
class Derived : public Base{
public : void derivedFunc( ) {
1. privBase = 5;
2. protBase = 10;
3. pubBase = 15;
}
};
class Der : public Derived {
public : void derFunc( ) {
4. privBase = 5;
5. protBase = 10;
6. pubBase = 15;
}
};
struct Base
{
public : int a;
};
struct Derived : Base
{
};
int main( )
{
Derived d;
d.a = 5;
}
Sikander 8
class Base
{
public : int a;
};
class Derived : Base
{
};
int main( )
{
Derived d;
d.a = 5;
}
struct Base
{
public : int a;
};
class Derived : Base
{
};
int main( )
{
Derived d;
d.a = 5;
}
Sikander 9
class Base
{
public : int a;
};
struct Derived : Base
{
};
int main( )
{
Derived d;
d.a = 5;
}
class Base {
public :
Base( ) { cout <<"Base DefaultConstructor n“; }
~Base( ) { cout <<"Base Destructor n“; }
};
class Derived : public Base
{
public : Derived( ) { cout <<"Derived Constructor n“; }
~Derived( ) { cout <<"Derived Destructor n“; }
};
int main( )
{
Derived dobj;
}
Sikander 10
class Base {
public :
Base( ) { cout <<"Base DefaultConstructor n“; }
Base(int x) { cout <<"Base Para Constructor n“; }
};
class Derived : public Base
{
public : Derived(int x)
{
cout <<"Derived Constructor n“;
}
};
int main( )
{
Derived dobj = Derived(5);
}
Sikander 11
class B2{
int b2;
};
Sikander 12
class B1{
int b1;
};
class D : public B1 , public B2 {
int d;
};
int main( ){
cout << sizeof(D) << endl;
}
class B2{
public : int b;
};
Sikander 13
class B1{
public : int b;
};
class D : public B1 , public B2 {
public : int d;
};
1. Can we inherit from two classes if they have members with same name?
2. Class D has two copies of b (1 from B1 and 1 from B2) or only one copy.
3. How to access the member b of B1 and member b of B2 in main.
class B2{
public : int b;
B2() {
b = 10;
}
};
Sikander 14
class B1{
public : int b;
B1( ){
b = 5;
}
};
class D : public B1 , public B2 {
public : int d;
void display( )
{
// Write code to Access value of b from class B1
// Write code to Access value of b from class B2
}
};
int main( )
{
D obj;
obj.display( );
}
class B2 : public B
{
};
Sikander 15
class B1 : public B
{
};
class D : public B1 , public B2 {
};
class B{
public : int arr[10];
};
int main( ){
cout << sizeof(B) << endl;
cout << sizeof(B1) << endl;
cout << sizeof(B2) << endl;
cout << sizeof(D) << endl;
}
class Player{
int playerid;
};
class Batsman : public Player {
int runs;
};
class Bowler : public Player {
int wickets;
};
class Allrounder : public Batsman , public Bowler
{
};
int main( )
{
cout << sizeof(Allrounder);
}
Sikander 16
class Player{
int playerid;
public : Player( ) { cout <<“Player Constructor n” };
};
class Batsman : public Player {
int runs;
};
class Bowler : public Player {
int wickets;
};
class Allrounder : public Batsman , public Bowler
{
};
int main( )
{
Allrounder ar1;
}
Sikander 17
class Base
{
public :
int base;
Base(int x = 0) : base ( x)
{ }
};
class Derived : public Base
{
public :
int der;
Derived(int x = 0) : der(x)
{ }
};
int main( )
{
Derived d(5);
cout << d.base << endl;
cout << d.der << endl;
}
Sikander 18
class Base {
public :
int base;
Base( ) : base(0)
{ cout <<"Base default Constructor “ <<endl; }
Base(int x) : base ( x)
{ cout <<"Base Parameterised Constructor " << x << endl; }
};
class Derived : public Base {
public : int der;
Derived(int x = 0) : der(x)
{ cout <<"Derived " << x << endl; }
};
int main( ) {
Derived d(5);
cout << d.base << endl;
cout << d.der << endl;
}
Sikander 19
class A{
public : void display( );
void print( );
};
Sikander 20
class B{
public : void display( );
void print(int x);
};
 Is print function overloaded?
 As we have two display functions with same
signature, will it throw an error?
classA{
public : void display( );
void print( );
};
Sikander 21
class B : public A{
public : void display( );
void print(int x);
};
 Is print function overloaded?
 As we have two display functions with same
signature, will it throw an error?
classA{
public : void display( );
void print( );
};
Sikander 22
class B : public A{
public : void display( );
void print(int x);
};
int main( )
{
B obj;
1. obj.print( );
2. obj.print(10);
}
classA{
public : void display( )
{
cout<<“Base Display”;
}
};
Sikander 23
class B : public A{
public :
void display( )
{
cout<<“Derived Display”;
}
};
int main( )
{
A tiger;
B cat;
tiger.display( );
cat.display();
tiger = cat; // Is this valid
tiger.display( );
}
class Base
{ protected : int bdata;
public : Base(int x ) { bdata = x; }
void display()
{ cout<<"n BASE DISPLAY bdata ="<<bdata<<endl; }
};
class Derived : public Base
{ private: int ddata;
public : Derived(int x ,int y ) : Base(x)
{ ddata = y; }
void display()
{
cout<<"n DERIVED DISPLAY "<<endl;
cout<<"bdata = "<<bdata<<endl;
cout<<"ddata = "<<ddata<<endl;
}
};
int main()
{
Base bObj(5);
Derived dObj(2,4);
bObj.display();
dObj.display();
bObj = dObj;
bObj.display();
return 0;
}
class Base
{
public :
void display()
{
cout<<“BASE FUNCTION "<<endl;
}
};
class Derived : public Base
{public:
void display()
{
cout<<“Derived Function "<<endl;
}
};
1. int main()
2. {
3. Base bObj;
4. Derived dObj;
5. Base *bptr;
6. Derived *dptr;
7. bptr = &dObj;
8. bptr->display( );
9. dptr = &bObj;
10. dptr->display( );
11. return 0;
12. }
class Base
{
public :
virtual void display()
{
cout<<“BASE FUNCTION "<<endl;
}
};
class Derived : public Base
{public:
void display()
{
cout<<“Derived Function "<<endl;
}
};
1. int main()
2. {
3. Base bObj;
4. Derived dObj;
5. Base *bptr;
6. Derived *dptr;
7. bptr = &bObj;
8. bptr->display( );
9. bptr = &bObj;
10. bptr->display( );
11. bObj = dObj;
12. bObj.display();
13. return 0;
14. }
class Base{
public : void display( )
{ cout <<"Base Display n“; }
};
class Derived : public Base {
public : void display( )
{ cout <<"Derived Display n“; }
};
int main( )
{
Base *pb;
pb = new Base;
pb->display( );
pb = new Derived;
pb->display( );
}
Sikander 27
classA{
public :
1. void display( );
2. void print( );
3. virtual void f1( );
4. virtual void read( );
5. virtual void access( );
6. void f2( );
};
Sikander 28
class B : public A{
public :
1. void display( );
2. void print(int x);
3. virtual void f1( );
4. void read( );
5. void access(int x);
6. virtual void f2( );
};
 When you create an Object dynamically, are constructors invoked?
 As the base pointer is pointing to dynamic object, will it invoke only base
constructor , only derived constructor or base and derived constructor.
 Is the destructor invoked?
Sikander 29
class Base
{
public : Base() {cout <<"Base Constructor n";}
public : ~Base() {cout <<"Base Destructor n";}
};
class Derived : public Base
{
public : Derived () {cout <<"Derived Constructor n";}
public : ~Derived () {cout <<"Derived Destructor n";}
};
int main()
{
Base *bp = new Derived();
}
class Base
{
public : Base() {cout <<"Base Constructor n";}
public : ~Base() {cout <<"Base Destructor n";}
};
class Derived : public Base
{
public : Derived () {cout <<"DerivedConstructor n";}
public : ~Derived () {cout <<"Derived Destructor n";}
};
int main()
{
Base *bp = new Derived();
delete bp;
}
Sikander 30
class Base
{
1. public : virtual Base( );
2. virtual ~Base();
3. virtual static void display( );
4. virtual void print( ) const ;
};
Sikander 31
class B{
public : virtual void display()
{
}
};
int main( )
{
cout << sizeof(B) << endl;
}
Sikander 32
class A{
public : void display()
{
}
};
int main( )
{
cout << sizeof(A) << endl;
}
class B{
int a;
int b;
public : virtual void display()
{
}
};
int main( )
{
cout << sizeof(B) << endl;
}
Sikander 33
class A{
int a;
int b;
public : void display()
{
}
};
int main( )
{
cout << sizeof(A) << endl;
}
class Student
{
int regno;
char name[20];
public :
virtual void read( );
virtual void display( );
virtual ~Student( );
};
int main( )
{
cout << sizeof(Student);
}
Sikander 34
class Base
{
public : virtual void display( ) {}
virtual ~Base( ) { }
};
class Derived : public Base
{
public : virtual void print( ) {}
};
int main( )
{
cout << sizeof(Base) << endl;
cout << sizeof(Derived) << endl;
}
Sikander 35
1. classA{
2. public : virtual void display( ) {
3. cout <<"A display n";
4. }
5. };
6. class B : publicA{
7. public : virtual void display( ) {
8. cout <<"B display n";
9. }
10. };
11. void myfunc(A &ref)
12. {
13. ref.display( );
14. }
15. int main( )
16. {
17. A a;
18. B b;
19. myfunc(a);
20. myfunc(b);
21. }
Sikander 36
1. When you receive by reference, will it
performObject Slicing.
2. Ref.display( ) will always invoke
function of classA or it depends of
type of object passed?
class Base
{
public : void display( );
virtual void print( );
virtual void read( );
};
class Derived : public Base
{
public : void display( );
virtual void print( );
};
Sikander 37
class Base
{
public : void display( );
virtual void print( );
virtual void read( );
};
class Derived : public Base
{
public : void display( );
virtual void print( );
};
Sikander 38
int main( )
{
Base *bptr = new Derived();
bptr->display( );
bptr->print( );
bptr->read( );
}
class Shape
{
public : int area( ) = 0;
};
class Rectangle : public Shape
{
int m_l , m_b;
public : Rectangle(int l , int b ) {
m_l = l , m_b = b;
}
int area( ){
return m_l * m_b;
}
};
Sikander 39
int main( )
{
Shape *ps = new Rectangle(4 , 5);
cout << ps->area( );
}
class Shape
{
public : virtual int area( ) = 0;
};
class Rectangle : public Shape
{
int m_l , m_b;
public : Rectangle(int l , int b ) {
m_l = l , m_b = b;
}
int area( ){
return m_l * m_b;
}
};
Sikander 40
int main( )
{
Rectangle r(4 , 5);
Shape &s = r;
cout << s.area( );
}
classA
{
public : virtual void display( ) = 0;
};
voidA::display( )
{
cout <<“A display n”;
}
int main( )
{
}
Sikander 41

More Related Content

What's hot

C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st StudyChris Ohk
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th StudyChris Ohk
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd StudyChris Ohk
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++Bharat Kalia
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th StudyChris Ohk
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...ssuserd6b1fd
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual finalAhalyaR
 
Travel management
Travel managementTravel management
Travel management1Parimal2
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...ssuserd6b1fd
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...ssuserd6b1fd
 

What's hot (20)

Container adapters
Container adaptersContainer adapters
Container adapters
 
Implementing string
Implementing stringImplementing string
Implementing string
 
Cquestions
Cquestions Cquestions
Cquestions
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
C++ file
C++ fileC++ file
C++ file
 
C programs
C programsC programs
C programs
 
C++ programs
C++ programsC++ programs
C++ programs
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
C program
C programC program
C program
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
informatics practices practical file
informatics practices practical fileinformatics practices practical file
informatics practices practical file
 
Travel management
Travel managementTravel management
Travel management
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
 
Array notes
Array notesArray notes
Array notes
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
 

Viewers also liked

Early warning signs Business Infoload 2016
Early warning signs   Business Infoload 2016Early warning signs   Business Infoload 2016
Early warning signs Business Infoload 2016Infoload
 
Anatomia arterias-ilovepdf-compressed
Anatomia arterias-ilovepdf-compressedAnatomia arterias-ilovepdf-compressed
Anatomia arterias-ilovepdf-compressedMishel Guerra
 
IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...
IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...
IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...Warnet Raha
 
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...Warnet Raha
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.CIIT Atd.
 
Indian media industry
Indian media industryIndian media industry
Indian media industryRajib jena
 
referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)
referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)
referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)Adeline Dlin
 
Kegawat daruratan pada neonatal
Kegawat daruratan pada neonatalKegawat daruratan pada neonatal
Kegawat daruratan pada neonatalIrma Delima
 
Kepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidan
Kepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidanKepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidan
Kepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidanIrfan Nur
 

Viewers also liked (14)

Early warning signs Business Infoload 2016
Early warning signs   Business Infoload 2016Early warning signs   Business Infoload 2016
Early warning signs Business Infoload 2016
 
Anatomia arterias-ilovepdf-compressed
Anatomia arterias-ilovepdf-compressedAnatomia arterias-ilovepdf-compressed
Anatomia arterias-ilovepdf-compressed
 
Kti restika oki lamorim
Kti restika oki lamorimKti restika oki lamorim
Kti restika oki lamorim
 
IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...
IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...
IDENTIFIKASI KARAKTERISTIK IBU YANG MENGALAMI PERDARAHAN HAMIL MUDA DI RUMAH ...
 
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN PADA BAYI Ny. I USIA 3 HARI D...
 
breastcare
breastcarebreastcare
breastcare
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
 
Kti rasmar yanti AKBID YKN BAU BAU
Kti rasmar yanti AKBID YKN BAU BAUKti rasmar yanti AKBID YKN BAU BAU
Kti rasmar yanti AKBID YKN BAU BAU
 
Indian media industry
Indian media industryIndian media industry
Indian media industry
 
Kti ratma ningsih
Kti ratma ningsihKti ratma ningsih
Kti ratma ningsih
 
referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)
referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)
referat obgyn resiko pada kehamilan (pembimbing : dr. Arie Widiyasa, spOG)
 
Selenium web driver in Java
Selenium web driver in JavaSelenium web driver in Java
Selenium web driver in Java
 
Kegawat daruratan pada neonatal
Kegawat daruratan pada neonatalKegawat daruratan pada neonatal
Kegawat daruratan pada neonatal
 
Kepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidan
Kepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidanKepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidan
Kepmenkes 836 menkes-sk-vi-2005-kinerja perawat dan bidan
 

Similar to Inheritance and polymorphism

C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)Ananda Kumar HN
 
C++ prgms 4th unit Inheritance
C++ prgms 4th unit InheritanceC++ prgms 4th unit Inheritance
C++ prgms 4th unit InheritanceAnanda Kumar HN
 
Inheritance_PART2.pptx
Inheritance_PART2.pptxInheritance_PART2.pptx
Inheritance_PART2.pptxArjunArora78
 
Inheritance compiler support
Inheritance compiler supportInheritance compiler support
Inheritance compiler supportSyed Zaid Irshad
 
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16Abu Saleh
 
Virtual function
Virtual functionVirtual function
Virtual functionharman kaur
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and ObjectsPayel Guria
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsBartosz Kosarzycki
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016STX Next
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.pptinstaface
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerGarth Gilmour
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 FeaturesJan Rüegg
 

Similar to Inheritance and polymorphism (20)

C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)
 
C++ prgms 4th unit Inheritance
C++ prgms 4th unit InheritanceC++ prgms 4th unit Inheritance
C++ prgms 4th unit Inheritance
 
Inheritance_PART2.pptx
Inheritance_PART2.pptxInheritance_PART2.pptx
Inheritance_PART2.pptx
 
Inheritance compiler support
Inheritance compiler supportInheritance compiler support
Inheritance compiler support
 
Unit 4
Unit 4Unit 4
Unit 4
 
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
 
Oop Presentation
Oop PresentationOop Presentation
Oop Presentation
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Lab3
Lab3Lab3
Lab3
 
Virtual function
Virtual functionVirtual function
Virtual function
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
Virtual Functions
Virtual FunctionsVirtual Functions
Virtual Functions
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projects
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
 
Sbaw091006
Sbaw091006Sbaw091006
Sbaw091006
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
 

Recently uploaded

Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 

Recently uploaded (20)

Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 

Inheritance and polymorphism

  • 1. Prepared by Mohammed Sikander Technical Lead Cranes Software mohammed.sikander@cranessoftware.com
  • 2. class Base{ private : int bpriv; protected : int bprot; Public : int bpub; }; class Derived : public Base{ }; Sikander 2 int main( ) { cout << sizeof(Derived) << endl; }
  • 3. class Base { }; class Derived : public Base { }; Sikander 3 class Base { int b; }; class Derived : public Base { }; class Base { }; class Derived : public Base { int d; };
  • 4. class Base { private : int privBase; protected : int protBase; public : int pubBase; }; class Derived : public Base { public : void derFunc( ) { 1. privBase = 5; 2. protBase = 10; 3. pubBase = 15; } }; Sikander 4 int main( ) { Derived dobj; 4. dobj.privBase = 5; 5. dobj.protBase = 10; 6. dobj.pubBase = 15; }
  • 5. class Base { private : int privBase; protected : int protBase; public : int pubBase; }; class Derived : protected Base { public : void derFunc( ) { 1. privBase = 5; 2. protBase = 10; 3. pubBase = 15; } }; Sikander 5 int main( ) { Derived dobj; 4. dobj.privBase = 5; 5. dobj.protBase = 10; 6. dobj.pubBase = 15; }
  • 6. class Base { private : int privBase; protected : int protBase; public : int pubBase; }; class Derived : private Base { public : void derFunc( ) { 1. privBase = 5; 2. protBase = 10; 3. pubBase = 15; } }; Sikander 6 int main( ) { Derived dobj; 4. dobj.privBase = 5; 5. dobj.protBase = 10; 6. dobj.pubBase = 15; }
  • 7. class Base { private : int privBase; protected : int protBase; public : int pubBase; }; class Derived : private Base{ public : void derivedFunc( ) { 1. privBase = 5; 2. protBase = 10; 3. pubBase = 15; } }; class Der : public Derived { public : void derFunc( ) { 4. privBase = 5; 5. protBase = 10; 6. pubBase = 15; } }; Sikander 7 class Base { private : int privBase; protected : int protBase; public : int pubBase; }; class Derived : public Base{ public : void derivedFunc( ) { 1. privBase = 5; 2. protBase = 10; 3. pubBase = 15; } }; class Der : public Derived { public : void derFunc( ) { 4. privBase = 5; 5. protBase = 10; 6. pubBase = 15; } };
  • 8. struct Base { public : int a; }; struct Derived : Base { }; int main( ) { Derived d; d.a = 5; } Sikander 8 class Base { public : int a; }; class Derived : Base { }; int main( ) { Derived d; d.a = 5; }
  • 9. struct Base { public : int a; }; class Derived : Base { }; int main( ) { Derived d; d.a = 5; } Sikander 9 class Base { public : int a; }; struct Derived : Base { }; int main( ) { Derived d; d.a = 5; }
  • 10. class Base { public : Base( ) { cout <<"Base DefaultConstructor n“; } ~Base( ) { cout <<"Base Destructor n“; } }; class Derived : public Base { public : Derived( ) { cout <<"Derived Constructor n“; } ~Derived( ) { cout <<"Derived Destructor n“; } }; int main( ) { Derived dobj; } Sikander 10
  • 11. class Base { public : Base( ) { cout <<"Base DefaultConstructor n“; } Base(int x) { cout <<"Base Para Constructor n“; } }; class Derived : public Base { public : Derived(int x) { cout <<"Derived Constructor n“; } }; int main( ) { Derived dobj = Derived(5); } Sikander 11
  • 12. class B2{ int b2; }; Sikander 12 class B1{ int b1; }; class D : public B1 , public B2 { int d; }; int main( ){ cout << sizeof(D) << endl; }
  • 13. class B2{ public : int b; }; Sikander 13 class B1{ public : int b; }; class D : public B1 , public B2 { public : int d; }; 1. Can we inherit from two classes if they have members with same name? 2. Class D has two copies of b (1 from B1 and 1 from B2) or only one copy. 3. How to access the member b of B1 and member b of B2 in main.
  • 14. class B2{ public : int b; B2() { b = 10; } }; Sikander 14 class B1{ public : int b; B1( ){ b = 5; } }; class D : public B1 , public B2 { public : int d; void display( ) { // Write code to Access value of b from class B1 // Write code to Access value of b from class B2 } }; int main( ) { D obj; obj.display( ); }
  • 15. class B2 : public B { }; Sikander 15 class B1 : public B { }; class D : public B1 , public B2 { }; class B{ public : int arr[10]; }; int main( ){ cout << sizeof(B) << endl; cout << sizeof(B1) << endl; cout << sizeof(B2) << endl; cout << sizeof(D) << endl; }
  • 16. class Player{ int playerid; }; class Batsman : public Player { int runs; }; class Bowler : public Player { int wickets; }; class Allrounder : public Batsman , public Bowler { }; int main( ) { cout << sizeof(Allrounder); } Sikander 16
  • 17. class Player{ int playerid; public : Player( ) { cout <<“Player Constructor n” }; }; class Batsman : public Player { int runs; }; class Bowler : public Player { int wickets; }; class Allrounder : public Batsman , public Bowler { }; int main( ) { Allrounder ar1; } Sikander 17
  • 18. class Base { public : int base; Base(int x = 0) : base ( x) { } }; class Derived : public Base { public : int der; Derived(int x = 0) : der(x) { } }; int main( ) { Derived d(5); cout << d.base << endl; cout << d.der << endl; } Sikander 18
  • 19. class Base { public : int base; Base( ) : base(0) { cout <<"Base default Constructor “ <<endl; } Base(int x) : base ( x) { cout <<"Base Parameterised Constructor " << x << endl; } }; class Derived : public Base { public : int der; Derived(int x = 0) : der(x) { cout <<"Derived " << x << endl; } }; int main( ) { Derived d(5); cout << d.base << endl; cout << d.der << endl; } Sikander 19
  • 20. class A{ public : void display( ); void print( ); }; Sikander 20 class B{ public : void display( ); void print(int x); };  Is print function overloaded?  As we have two display functions with same signature, will it throw an error?
  • 21. classA{ public : void display( ); void print( ); }; Sikander 21 class B : public A{ public : void display( ); void print(int x); };  Is print function overloaded?  As we have two display functions with same signature, will it throw an error?
  • 22. classA{ public : void display( ); void print( ); }; Sikander 22 class B : public A{ public : void display( ); void print(int x); }; int main( ) { B obj; 1. obj.print( ); 2. obj.print(10); }
  • 23. classA{ public : void display( ) { cout<<“Base Display”; } }; Sikander 23 class B : public A{ public : void display( ) { cout<<“Derived Display”; } }; int main( ) { A tiger; B cat; tiger.display( ); cat.display(); tiger = cat; // Is this valid tiger.display( ); }
  • 24. class Base { protected : int bdata; public : Base(int x ) { bdata = x; } void display() { cout<<"n BASE DISPLAY bdata ="<<bdata<<endl; } }; class Derived : public Base { private: int ddata; public : Derived(int x ,int y ) : Base(x) { ddata = y; } void display() { cout<<"n DERIVED DISPLAY "<<endl; cout<<"bdata = "<<bdata<<endl; cout<<"ddata = "<<ddata<<endl; } }; int main() { Base bObj(5); Derived dObj(2,4); bObj.display(); dObj.display(); bObj = dObj; bObj.display(); return 0; }
  • 25. class Base { public : void display() { cout<<“BASE FUNCTION "<<endl; } }; class Derived : public Base {public: void display() { cout<<“Derived Function "<<endl; } }; 1. int main() 2. { 3. Base bObj; 4. Derived dObj; 5. Base *bptr; 6. Derived *dptr; 7. bptr = &dObj; 8. bptr->display( ); 9. dptr = &bObj; 10. dptr->display( ); 11. return 0; 12. }
  • 26. class Base { public : virtual void display() { cout<<“BASE FUNCTION "<<endl; } }; class Derived : public Base {public: void display() { cout<<“Derived Function "<<endl; } }; 1. int main() 2. { 3. Base bObj; 4. Derived dObj; 5. Base *bptr; 6. Derived *dptr; 7. bptr = &bObj; 8. bptr->display( ); 9. bptr = &bObj; 10. bptr->display( ); 11. bObj = dObj; 12. bObj.display(); 13. return 0; 14. }
  • 27. class Base{ public : void display( ) { cout <<"Base Display n“; } }; class Derived : public Base { public : void display( ) { cout <<"Derived Display n“; } }; int main( ) { Base *pb; pb = new Base; pb->display( ); pb = new Derived; pb->display( ); } Sikander 27
  • 28. classA{ public : 1. void display( ); 2. void print( ); 3. virtual void f1( ); 4. virtual void read( ); 5. virtual void access( ); 6. void f2( ); }; Sikander 28 class B : public A{ public : 1. void display( ); 2. void print(int x); 3. virtual void f1( ); 4. void read( ); 5. void access(int x); 6. virtual void f2( ); };
  • 29.  When you create an Object dynamically, are constructors invoked?  As the base pointer is pointing to dynamic object, will it invoke only base constructor , only derived constructor or base and derived constructor.  Is the destructor invoked? Sikander 29 class Base { public : Base() {cout <<"Base Constructor n";} public : ~Base() {cout <<"Base Destructor n";} }; class Derived : public Base { public : Derived () {cout <<"Derived Constructor n";} public : ~Derived () {cout <<"Derived Destructor n";} }; int main() { Base *bp = new Derived(); }
  • 30. class Base { public : Base() {cout <<"Base Constructor n";} public : ~Base() {cout <<"Base Destructor n";} }; class Derived : public Base { public : Derived () {cout <<"DerivedConstructor n";} public : ~Derived () {cout <<"Derived Destructor n";} }; int main() { Base *bp = new Derived(); delete bp; } Sikander 30
  • 31. class Base { 1. public : virtual Base( ); 2. virtual ~Base(); 3. virtual static void display( ); 4. virtual void print( ) const ; }; Sikander 31
  • 32. class B{ public : virtual void display() { } }; int main( ) { cout << sizeof(B) << endl; } Sikander 32 class A{ public : void display() { } }; int main( ) { cout << sizeof(A) << endl; }
  • 33. class B{ int a; int b; public : virtual void display() { } }; int main( ) { cout << sizeof(B) << endl; } Sikander 33 class A{ int a; int b; public : void display() { } }; int main( ) { cout << sizeof(A) << endl; }
  • 34. class Student { int regno; char name[20]; public : virtual void read( ); virtual void display( ); virtual ~Student( ); }; int main( ) { cout << sizeof(Student); } Sikander 34
  • 35. class Base { public : virtual void display( ) {} virtual ~Base( ) { } }; class Derived : public Base { public : virtual void print( ) {} }; int main( ) { cout << sizeof(Base) << endl; cout << sizeof(Derived) << endl; } Sikander 35
  • 36. 1. classA{ 2. public : virtual void display( ) { 3. cout <<"A display n"; 4. } 5. }; 6. class B : publicA{ 7. public : virtual void display( ) { 8. cout <<"B display n"; 9. } 10. }; 11. void myfunc(A &ref) 12. { 13. ref.display( ); 14. } 15. int main( ) 16. { 17. A a; 18. B b; 19. myfunc(a); 20. myfunc(b); 21. } Sikander 36 1. When you receive by reference, will it performObject Slicing. 2. Ref.display( ) will always invoke function of classA or it depends of type of object passed?
  • 37. class Base { public : void display( ); virtual void print( ); virtual void read( ); }; class Derived : public Base { public : void display( ); virtual void print( ); }; Sikander 37
  • 38. class Base { public : void display( ); virtual void print( ); virtual void read( ); }; class Derived : public Base { public : void display( ); virtual void print( ); }; Sikander 38 int main( ) { Base *bptr = new Derived(); bptr->display( ); bptr->print( ); bptr->read( ); }
  • 39. class Shape { public : int area( ) = 0; }; class Rectangle : public Shape { int m_l , m_b; public : Rectangle(int l , int b ) { m_l = l , m_b = b; } int area( ){ return m_l * m_b; } }; Sikander 39 int main( ) { Shape *ps = new Rectangle(4 , 5); cout << ps->area( ); }
  • 40. class Shape { public : virtual int area( ) = 0; }; class Rectangle : public Shape { int m_l , m_b; public : Rectangle(int l , int b ) { m_l = l , m_b = b; } int area( ){ return m_l * m_b; } }; Sikander 40 int main( ) { Rectangle r(4 , 5); Shape &s = r; cout << s.area( ); }
  • 41. classA { public : virtual void display( ) = 0; }; voidA::display( ) { cout <<“A display n”; } int main( ) { } Sikander 41