SlideShare a Scribd company logo
1 of 5
Download to read offline
Object Oriented Programming with C++ Inheritance – I
1 | P a g e A n a n d a K u m a r H N
UNIT - 04 / Inheritance – I
Syntax:
class derived_class_name : visibility_mode base_class_name
{
//body of derived class
}
Example:
class BASE
{
private:
protected:
public:
}
class DERIVED : public BASE
{
private:
protected:
public:
}
Single Inheritance: public
class base
{
private:
int a,b;
public:
void initbase(int x,int y)
{
a=x;
b=y;
}
void showbase( )
{
cout<<"a="<<a<<" b="<<b<<endl;
}
};
class derived:public base
{
private:
int m,n;
public:
void initderived(int u,int v)
{
m=u;
n=v;
}
void showderived( )
{
cout<<"m="<<m<<" n="<<n<<endl;
}
};
int main( )
{
base b1; //object of base class
b1.initbase(10,20);
b1.showbase( );
derived d1; //object of derived class
d1.initderived(33,44);
d1.showderived( );
d1.initbase(2,3); //member function of base class is
accessible from object of derived class
d1.showbase( );
}
OUTPUT:
Single Inheritance: private
class base
{
private:
int a,b;
public:
void initbase(int x,int y)
{
a=x;
b=y;
}
void showbase( )
{
cout<<"a="<<a<<" b="<<b<<endl;
}
};
class derived:private base
{
private:
int m,n;
Object Oriented Programming with C++ Inheritance – I
2 | P a g e A n a n d a K u m a r H N
public:
void initderived(int u,int v)
{
m=u;
n=v;
}
void showderived( )
{
cout<<"m="<<m<<" n="<<n<<endl;
}
};
int main()
{
base b1;
b1.initbase(10,20);
b1.showbase( );
derived d1;
d1.initderived(33,44);
d1.showderived( );
d1.initbase(2,3); //not accessible since it is
privately inherited
d1.showbase( ); //not accessible
}
ERROR:
class base
{
private:
int a,b;
public:
void initbase(int x,int y)
{
a=x;
b=y;
}
void showbase( )
{
cout<<"a="<<a<<" b="<<b<<endl;
}
};
class derived:private base
{
private:
int m,n;
public:
void initderived(int u,int v)
{
initbase(u,v);
m=u;
n=v;
}
void showderived( )
{
showbase( );
cout<<"m="<<m<<" n="<<n<<endl;
}
};
int main( )
{
base b1;
b1.initbase(10,20);
b1.showbase( );
derived d1;
d1.initderived(33,44);
d1.showderived( );
}
OUTPUT:
Inheritance and protected members:
class x
{
protected:
int i,j;
void display()
{
cout<<i<<j;
}
};
int main()
{
x x1;
cout<<"enter i j";
cin>>x1.i>>x1.j; //ERROR; cannot access
x1.display();
}
ERROR:
Object Oriented Programming with C++ Inheritance – I
3 | P a g e A n a n d a K u m a r H N
//accessing protected members of base class
Example 1:
class base
{
protected:
int i,j; //protected data members
public:
void setbase(int x,int y)
{
i=x;
j=y;
}
void showbase( )
{
cout<<"i="<<i<<" j="<<j<<endl;
}
};
class derived: public base
{
int k;
public:
void setk( )
{
k=i*j; //accessing protected members
}
void showderived( )
{
cout<<"k="<<k<<endl;
}
};
int main()
{
derived d1;
d1.setbase(10,12);
d1.showbase( );
d1.setk( );
d1.showderived( );
}
OUTPUT:
Example 2:
class x
{
protected:
int i,j;
void display( )
{
cout<<i<<endl<<j;
}
};
class y: public x
{
public:
void init(int x,int y)
{
i=x;
j=y;
}
void show( )
{
display( );
}
};
int main()
{
y y1;
y1.init(10,90);
y1.show( );
}
OUTPUT:
10
90
Protected base class inheritance:
/*public and protected data becomes
protected members of derived class*/
class base
{
protected:
int i,j;
public:
void setbase(int x,int y)
{
i=x;
j=y;
}
void showbase( )
{
cout<<"i="<<i<<" j="<<j<<endl;
}
};
class derived: protected base
{
int k;
public:
void setk(int x,int y)
{
setbase(x,y);
k=i*j; //accessing protected data
}
Object Oriented Programming with C++ Inheritance – I
4 | P a g e A n a n d a K u m a r H N
void showderived()
{
showbase( );
cout<<"k="<<k<<endl;
}
};
int main( )
{
derived d1;
d1.setk(33,3);
d1.showderived( );
d1.setbase( ) // ERROR; protected member
of derived class
d1.showbase( ) // ERROR;
}
OUTPUT:
Visibility Mode:
Access Type
of Member
(in Base
class)
Visibility Mode ( in Derived Class)
Private Protected Public
Private ------------Not Accessible-----------------
Protected Private Protected Protected
Public Private Protected Public
Multiple Inheritances:
class x
{
public:
int i,j;
void displayx()
{
cout<<i<<endl<<j;
}
};
class y
{
public:
int m,n;
void displayy()
{
cout<<endl<<endl;
cout<<m<<endl<<n;
}
};
class z: public x,public y
{
public:
void read()
{
cout<<"enter i j m n";
cin>>i>>j>>m>>n;
}
void displayz()
{
displayx();
displayy();
}
};
int main()
{
z z1;
z1.read();
z1.displayz();
}
OUTPUT:
/* Problem with multiple inheritance
And solving the problem using :: operator*/
class x
{
public:
int i,j;
void display()
{ cout<<"nin class xn";
cout<<i<<endl<<j;
}
};
class y
{
public:
int m,n;
void display()
{
cout<<"nin class yn";
cout<<m<<endl<<n;
}
};
Object Oriented Programming with C++ Inheritance – I
5 | P a g e A n a n d a K u m a r H N
class z: public x,public y
{
public:
void read()
{
cout<<"nenter i j m n";
cin>>i>>j>>m>>n;
}
void display()
{
cout<<"nin class zn";
x::display();
y::display();
}
};
int main()
{
z z1;
z1.read();
z1.display();
z1.x::display();
z1.y::display();
}
OUTPUT:
By using :: operator ambiguity in calling functions of
base classes in multiple inheritance can be resolved.

More Related Content

What's hot

CQRS and Event Sourcing in a Symfony application
CQRS and Event Sourcing in a Symfony applicationCQRS and Event Sourcing in a Symfony application
CQRS and Event Sourcing in a Symfony applicationSamuel ROZE
 
Drupal Field API. Practical usage
Drupal Field API. Practical usageDrupal Field API. Practical usage
Drupal Field API. Practical usagePavel Makhrinsky
 
Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"
Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"
Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"ZendCon
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphismmohamed sikander
 
Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019julien pauli
 
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsAdding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsSam Hennessy
 
Drupal 8: Routing & More
Drupal 8: Routing & MoreDrupal 8: Routing & More
Drupal 8: Routing & Moredrubb
 
Dependency Injection in Laravel
Dependency Injection in LaravelDependency Injection in Laravel
Dependency Injection in LaravelHAO-WEN ZHANG
 
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo....NET Conf UY
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual finalAhalyaR
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Fabien Potencier
 
Objective-C Crash Course for Web Developers
Objective-C Crash Course for Web DevelopersObjective-C Crash Course for Web Developers
Objective-C Crash Course for Web DevelopersJoris Verbogt
 
Introduction to CQRS and Event Sourcing
Introduction to CQRS and Event SourcingIntroduction to CQRS and Event Sourcing
Introduction to CQRS and Event SourcingSamuel ROZE
 
Top Ten Reasons to Use EntityFieldQuery in Drupal
Top Ten Reasons to Use EntityFieldQuery in DrupalTop Ten Reasons to Use EntityFieldQuery in Drupal
Top Ten Reasons to Use EntityFieldQuery in DrupalFredric Mitchell
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Fabien Potencier
 
Tutconstructordes
TutconstructordesTutconstructordes
TutconstructordesNiti Arora
 

What's hot (20)

CQRS and Event Sourcing in a Symfony application
CQRS and Event Sourcing in a Symfony applicationCQRS and Event Sourcing in a Symfony application
CQRS and Event Sourcing in a Symfony application
 
Opp compile
Opp compileOpp compile
Opp compile
 
Drupal Field API. Practical usage
Drupal Field API. Practical usageDrupal Field API. Practical usage
Drupal Field API. Practical usage
 
Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"
Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"
Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
 
Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019
 
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsAdding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
 
Presentation1
Presentation1Presentation1
Presentation1
 
Drupal 8: Routing & More
Drupal 8: Routing & MoreDrupal 8: Routing & More
Drupal 8: Routing & More
 
Dependency Injection in Laravel
Dependency Injection in LaravelDependency Injection in Laravel
Dependency Injection in Laravel
 
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
Lecture19
Lecture19Lecture19
Lecture19
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
 
Objective-C Crash Course for Web Developers
Objective-C Crash Course for Web DevelopersObjective-C Crash Course for Web Developers
Objective-C Crash Course for Web Developers
 
Introduction to CQRS and Event Sourcing
Introduction to CQRS and Event SourcingIntroduction to CQRS and Event Sourcing
Introduction to CQRS and Event Sourcing
 
Top Ten Reasons to Use EntityFieldQuery in Drupal
Top Ten Reasons to Use EntityFieldQuery in DrupalTop Ten Reasons to Use EntityFieldQuery in Drupal
Top Ten Reasons to Use EntityFieldQuery in Drupal
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010
 
Property-based testing
Property-based testingProperty-based testing
Property-based testing
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
 

Similar to C++ prgms 4th unit Inheritance

Similar to C++ prgms 4th unit Inheritance (20)

Lab3
Lab3Lab3
Lab3
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
 
Simple example program for inheritance in c++
Simple example program for inheritance in c++Simple example program for inheritance in c++
Simple example program for inheritance in c++
 
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++
 
C++ programs
C++ programsC++ programs
C++ programs
 
lecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdflecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdf
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture4
Lecture4Lecture4
Lecture4
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Inheritance
InheritanceInheritance
Inheritance
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
Inheritance
InheritanceInheritance
Inheritance
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
 
Presentation on template and exception
Presentation  on template and exceptionPresentation  on template and exception
Presentation on template and exception
 
Pre zen ta sion
Pre zen ta sionPre zen ta sion
Pre zen ta sion
 
10 inheritance
10 inheritance10 inheritance
10 inheritance
 

More from Ananda Kumar HN

VTU Network lab programs
VTU Network lab   programsVTU Network lab   programs
VTU Network lab programsAnanda Kumar HN
 
VTU CN-1important questions
VTU CN-1important questionsVTU CN-1important questions
VTU CN-1important questionsAnanda Kumar HN
 
Oop with c++ notes unit 01 introduction
Oop with c++ notes   unit 01 introductionOop with c++ notes   unit 01 introduction
Oop with c++ notes unit 01 introductionAnanda Kumar HN
 
C++ prgms io file unit 7
C++ prgms io file unit 7C++ prgms io file unit 7
C++ prgms io file unit 7Ananda Kumar HN
 
Microsoft office in kannada for begineers
Microsoft office in kannada for begineersMicrosoft office in kannada for begineers
Microsoft office in kannada for begineersAnanda Kumar HN
 
Microsoft office in KANNADA
Microsoft office in KANNADAMicrosoft office in KANNADA
Microsoft office in KANNADAAnanda Kumar HN
 

More from Ananda Kumar HN (9)

DAA 18CS42 VTU CSE
DAA 18CS42 VTU CSEDAA 18CS42 VTU CSE
DAA 18CS42 VTU CSE
 
CGV 18CS62 VTU CSE
CGV 18CS62 VTU CSECGV 18CS62 VTU CSE
CGV 18CS62 VTU CSE
 
Python for beginners
Python for beginnersPython for beginners
Python for beginners
 
VTU Network lab programs
VTU Network lab   programsVTU Network lab   programs
VTU Network lab programs
 
VTU CN-1important questions
VTU CN-1important questionsVTU CN-1important questions
VTU CN-1important questions
 
Oop with c++ notes unit 01 introduction
Oop with c++ notes   unit 01 introductionOop with c++ notes   unit 01 introduction
Oop with c++ notes unit 01 introduction
 
C++ prgms io file unit 7
C++ prgms io file unit 7C++ prgms io file unit 7
C++ prgms io file unit 7
 
Microsoft office in kannada for begineers
Microsoft office in kannada for begineersMicrosoft office in kannada for begineers
Microsoft office in kannada for begineers
 
Microsoft office in KANNADA
Microsoft office in KANNADAMicrosoft office in KANNADA
Microsoft office in KANNADA
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 

C++ prgms 4th unit Inheritance

  • 1. Object Oriented Programming with C++ Inheritance – I 1 | P a g e A n a n d a K u m a r H N UNIT - 04 / Inheritance – I Syntax: class derived_class_name : visibility_mode base_class_name { //body of derived class } Example: class BASE { private: protected: public: } class DERIVED : public BASE { private: protected: public: } Single Inheritance: public class base { private: int a,b; public: void initbase(int x,int y) { a=x; b=y; } void showbase( ) { cout<<"a="<<a<<" b="<<b<<endl; } }; class derived:public base { private: int m,n; public: void initderived(int u,int v) { m=u; n=v; } void showderived( ) { cout<<"m="<<m<<" n="<<n<<endl; } }; int main( ) { base b1; //object of base class b1.initbase(10,20); b1.showbase( ); derived d1; //object of derived class d1.initderived(33,44); d1.showderived( ); d1.initbase(2,3); //member function of base class is accessible from object of derived class d1.showbase( ); } OUTPUT: Single Inheritance: private class base { private: int a,b; public: void initbase(int x,int y) { a=x; b=y; } void showbase( ) { cout<<"a="<<a<<" b="<<b<<endl; } }; class derived:private base { private: int m,n;
  • 2. Object Oriented Programming with C++ Inheritance – I 2 | P a g e A n a n d a K u m a r H N public: void initderived(int u,int v) { m=u; n=v; } void showderived( ) { cout<<"m="<<m<<" n="<<n<<endl; } }; int main() { base b1; b1.initbase(10,20); b1.showbase( ); derived d1; d1.initderived(33,44); d1.showderived( ); d1.initbase(2,3); //not accessible since it is privately inherited d1.showbase( ); //not accessible } ERROR: class base { private: int a,b; public: void initbase(int x,int y) { a=x; b=y; } void showbase( ) { cout<<"a="<<a<<" b="<<b<<endl; } }; class derived:private base { private: int m,n; public: void initderived(int u,int v) { initbase(u,v); m=u; n=v; } void showderived( ) { showbase( ); cout<<"m="<<m<<" n="<<n<<endl; } }; int main( ) { base b1; b1.initbase(10,20); b1.showbase( ); derived d1; d1.initderived(33,44); d1.showderived( ); } OUTPUT: Inheritance and protected members: class x { protected: int i,j; void display() { cout<<i<<j; } }; int main() { x x1; cout<<"enter i j"; cin>>x1.i>>x1.j; //ERROR; cannot access x1.display(); } ERROR:
  • 3. Object Oriented Programming with C++ Inheritance – I 3 | P a g e A n a n d a K u m a r H N //accessing protected members of base class Example 1: class base { protected: int i,j; //protected data members public: void setbase(int x,int y) { i=x; j=y; } void showbase( ) { cout<<"i="<<i<<" j="<<j<<endl; } }; class derived: public base { int k; public: void setk( ) { k=i*j; //accessing protected members } void showderived( ) { cout<<"k="<<k<<endl; } }; int main() { derived d1; d1.setbase(10,12); d1.showbase( ); d1.setk( ); d1.showderived( ); } OUTPUT: Example 2: class x { protected: int i,j; void display( ) { cout<<i<<endl<<j; } }; class y: public x { public: void init(int x,int y) { i=x; j=y; } void show( ) { display( ); } }; int main() { y y1; y1.init(10,90); y1.show( ); } OUTPUT: 10 90 Protected base class inheritance: /*public and protected data becomes protected members of derived class*/ class base { protected: int i,j; public: void setbase(int x,int y) { i=x; j=y; } void showbase( ) { cout<<"i="<<i<<" j="<<j<<endl; } }; class derived: protected base { int k; public: void setk(int x,int y) { setbase(x,y); k=i*j; //accessing protected data }
  • 4. Object Oriented Programming with C++ Inheritance – I 4 | P a g e A n a n d a K u m a r H N void showderived() { showbase( ); cout<<"k="<<k<<endl; } }; int main( ) { derived d1; d1.setk(33,3); d1.showderived( ); d1.setbase( ) // ERROR; protected member of derived class d1.showbase( ) // ERROR; } OUTPUT: Visibility Mode: Access Type of Member (in Base class) Visibility Mode ( in Derived Class) Private Protected Public Private ------------Not Accessible----------------- Protected Private Protected Protected Public Private Protected Public Multiple Inheritances: class x { public: int i,j; void displayx() { cout<<i<<endl<<j; } }; class y { public: int m,n; void displayy() { cout<<endl<<endl; cout<<m<<endl<<n; } }; class z: public x,public y { public: void read() { cout<<"enter i j m n"; cin>>i>>j>>m>>n; } void displayz() { displayx(); displayy(); } }; int main() { z z1; z1.read(); z1.displayz(); } OUTPUT: /* Problem with multiple inheritance And solving the problem using :: operator*/ class x { public: int i,j; void display() { cout<<"nin class xn"; cout<<i<<endl<<j; } }; class y { public: int m,n; void display() { cout<<"nin class yn"; cout<<m<<endl<<n; } };
  • 5. Object Oriented Programming with C++ Inheritance – I 5 | P a g e A n a n d a K u m a r H N class z: public x,public y { public: void read() { cout<<"nenter i j m n"; cin>>i>>j>>m>>n; } void display() { cout<<"nin class zn"; x::display(); y::display(); } }; int main() { z z1; z1.read(); z1.display(); z1.x::display(); z1.y::display(); } OUTPUT: By using :: operator ambiguity in calling functions of base classes in multiple inheritance can be resolved.