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

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 

Recently uploaded (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 

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.