SlideShare a Scribd company logo
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

PDF
Implementing stack
PDF
C++ Question on References and Function Overloading
PDF
Static and const members
PDF
Understanding storage class using nm
PDF
Stl algorithm-Basic types
PPTX
Function basics
PDF
Polymorphism
PDF
Operator overloading
Implementing stack
C++ Question on References and Function Overloading
Static and const members
Understanding storage class using nm
Stl algorithm-Basic types
Function basics
Polymorphism
Operator overloading

What's hot (20)

PDF
Container adapters
PDF
Implementing string
PPT
Cquestions
PDF
C++ Programming - 1st Study
PDF
C++ Programming - 11th Study
DOCX
C++ file
PDF
C programs
PDF
C++ programs
PDF
C++ Programming - 2nd Study
DOCX
Basic Programs of C++
PDF
C program
PDF
C++ Programming - 4th Study
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
PPTX
C sharp 8
DOC
Pads lab manual final
DOCX
informatics practices practical file
DOCX
Travel management
PDF
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
PDF
Array notes
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Container adapters
Implementing string
Cquestions
C++ Programming - 1st Study
C++ Programming - 11th Study
C++ file
C programs
C++ programs
C++ Programming - 2nd Study
Basic Programs of C++
C program
C++ Programming - 4th Study
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
C sharp 8
Pads lab manual final
informatics practices practical file
Travel management
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Array notes
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Ad

Viewers also liked (14)

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

Similar to Inheritance and polymorphism (20)

PDF
Inheritance chapter-6-computer-science-with-c++ opt
PPT
Inheritance
PDF
Chapter 6 and inheritance OOP C++ tu ioe
PPT
inhertance c++
PDF
Object Oriented Programming (OOP) using C++ - Lecture 3
PPTX
inheritance c++
PPT
Inheritance
PPTX
OOP unit II inheritance.pptx object oriented programming
PDF
lecture-2021inheritance-160705095417.pdf
PPTX
[OOP - Lec 20,21] Inheritance
PPTX
OOPS Basics With Example
PPTX
Inheritance
PPTX
Inheritance
PDF
C++ prgms 5th unit (inheritance ii)
PDF
C++ prgms 3rd unit
PPT
Lec 42.43 - virtual.functions
PPT
OOPs Lecture 2
PDF
OOPs theory about its concepts and properties.
PPT
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance
Chapter 6 and inheritance OOP C++ tu ioe
inhertance c++
Object Oriented Programming (OOP) using C++ - Lecture 3
inheritance c++
Inheritance
OOP unit II inheritance.pptx object oriented programming
lecture-2021inheritance-160705095417.pdf
[OOP - Lec 20,21] Inheritance
OOPS Basics With Example
Inheritance
Inheritance
C++ prgms 5th unit (inheritance ii)
C++ prgms 3rd unit
Lec 42.43 - virtual.functions
OOPs Lecture 2
OOPs theory about its concepts and properties.

Recently uploaded (20)

PDF
medical staffing services at VALiNTRY
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Introduction to Artificial Intelligence
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
AI in Product Development-omnex systems
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Digital Strategies for Manufacturing Companies
PDF
Nekopoi APK 2025 free lastest update
PPTX
history of c programming in notes for students .pptx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
CHAPTER 2 - PM Management and IT Context
medical staffing services at VALiNTRY
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Understanding Forklifts - TECH EHS Solution
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
2025 Textile ERP Trends: SAP, Odoo & Oracle
Wondershare Filmora 15 Crack With Activation Key [2025
Introduction to Artificial Intelligence
How Creative Agencies Leverage Project Management Software.pdf
AI in Product Development-omnex systems
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Navsoft: AI-Powered Business Solutions & Custom Software Development
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Digital Strategies for Manufacturing Companies
Nekopoi APK 2025 free lastest update
history of c programming in notes for students .pptx
Design an Analysis of Algorithms II-SECS-1021-03
CHAPTER 2 - PM Management and IT Context

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