0% found this document useful (0 votes)
12 views

Lecture Friend

Uploaded by

abushahzad258
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lecture Friend

Uploaded by

abushahzad258
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Class in C++

Friend Class
Friend Class
Friend Function
• // Example program
• #include <iostream>
• #include <string>
• using namespace std;

• class car
• {
• private:
• int car_no;
• string car_name;
• public:
• car()
• {
• car_no=01;
• car_name="NEXA";
• cout<<car_no;
• cout<<car_name;
• }

• friend void bestcar(car &c);

• };

• void bestcar(car &c)
• {
• c.car_no=002;
• c.car_name="HONDA BRV";
• cout<<car_name;
• }
• int main()
• {

• car c;
• bestcar(c);


• }
Home Work
• One example for Friend Class.
What Is Inheritance?
• Provides a way to create a new class from an
existing class
• The new class is a specialized version of the
existing class

CS1 -- Inheritance and Polymorphism 7


Example: Insect Taxonomy

CS1 -- Inheritance and Polymorphism 8


The "is a" Relationship
• Inheritance establishes an "is a"
relationship between classes.
– A poodle is a dog
– A car is a vehicle
– A flower is a plant
– A football player is an athlete

CS1 -- Inheritance and Polymorphism 9


Inheritance – Terminology and Notation in
C++
• Base class (or parent) – inherited from
• Derived class (or child) – inherits from the base class
• Notation:
class Student // base class
{
. . .
};
class UnderGrad : public student
{ // derived class
. . .
};
CS1 -- Inheritance and Polymorphism 10
Constructors and Destructors in Base and
Derived Classes
• Derived classes can have their own
constructors and destructors
• When an object of a derived class is created,
the base class’s constructor is executed first,
followed by the derived class’s constructor
• When an object of a derived class is
destroyed, its destructor is called first, then
that of the base class

CS1 -- Inheritance and Polymorphism 11


Constructors and Destructors in Base and
Derived Classes

CS1 -- Inheritance and Polymorphism 12


Constructors and Destructors in Base and
Derived Classes

CS1 -- Inheritance and Polymorphism 13


Constructors and Destructors in Base and
Derived Classes

CS1 -- Inheritance and Polymorphism 14


Home Work
• What is a virtual Function?
• Assignment No 02 Uploaded on LMS.

You might also like