Unit-2 Inheritance & Pointers
Unit-2 Inheritance & Pointers
24-11-2022 1
UNIT-2
INHERITANCE & POINTERS
Super Class(Parent): The class whose properties are inherited by sub class is called Base
Class or Super class.
void main( )
{
clrscr( );
emp s;
s.getdata1( );
s.putdata1();
getch();
}
Output:
• A class in C++ has public, private and protected sections which contain the corresponding
class members.
• Protected members in a class are similar to private members as they cannot be accessed
from outside the class.
• But they can be accessed by derived classes or child classes while private members cannot.
int main()
{
Derived obj;
obj.func();
return 0;
}
Output:
The value of num is: 7
A destructor as the name implies is used to destroy the objects that have been created by a
constructor.
The destructor is a member function whose name is the same as the class name but is the
same as the class name but is preceded by a tilde.
~ sample( ) //destructor
{
•Function overriding in C++ is a feature that allows us to use a function in the child class
that is already present in its parent class.
•The child class inherits all the data members, and the member functions present in the
parent class.
Vehicle
Car Bike
Public Inheritance means derived class can access the public members of base class.
•Example
Public keyword is above statement signifies all public members of base class are accessible
in the derive class.
Private keyword in above statement signifies no member of base class will be accessible.
•Example:
Class derived : private base
UNIT-II Inheritance & Pointer
24-11-2022 27
Department Of Computer Engineering
1 Inheritance.
2.8 Member Access Control:
•This situation arises when multiple base class have members with same name.
•Derived class faces the ambiguity as which base class version of the member to use.
•At the time of calling the member we can specify the name class from we wanted to access
the members.
Object _name.BASE::abc()
UNIT-II Inheritance & Pointer
24-11-2022 39
Department Of Computer Engineering
OBJECT ORIENTED PROGRAM:
Example:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class company
{
char Com_name[30];
int year;
public:
void getdata();
void putdata();
};
void company :: getdata()
{ cout<<"\n Enter Name of Company:";
cin>>Com_name;
cout<<"\n Enter Year of Company: ";
cin>>year;
}
void company::putdata()
{ cout<<"\n Name Of company:"<<Com_name;
cout<<"\n Year Of Company:"<<year;
UNIT-V Object Oriented Programming 24-11-2022
}; Department Of Computer Engineering
40
OBJECT ORIENTED PROGRAM:
class worker
{
char name[30];
int age;
public:
void getdata()
{
cout<<"\n Enter Name of Employee:";
cin>>name;
cout<<"\n Enter Age of Employee:";
cin>>age;
}
void putdata()
{ cout<<"\n Name of Employee:"<<name;
cout<<"\n Age of Employee:"<<age;
}
};
class product: public company, public worker
{
char prod_name[30];
float price;
UNIT-V Object Oriented Programming 24-11-2022
public: Department Of Computer Engineering
41
OBJECT ORIENTED PROGRAM:
void get1()
{
//getdata();
//get();
cout<<"\n Enter the product name:";
cin>>prod_name;
cout<<"\n Enter the price:";
cin>>price;
}
void put1()
{
//putdata();
//put();
cout<<"\n The Product name is:"<<prod_name;
cout<<"\n The Product Price:"<<price;
}
};
UNIT-V Object Oriented Programming 24-11-2022 42
Department Of Computer Engineering
OBJECT ORIENTED PROGRAM:
void main()
{
clrscr();
cout<<"\n Ambiguity in multiple inheritance";
product p1;
p1.get1();
p1.company::getdata();// ambiguity in multiple inheritance
p1.worker::getdata();
p1.company::putdata();
p1.worker::putdata();
p1.put1();
getch();
}
•Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a
given class appearing in an inheritance hierarchy when using multiple inheritances.
•This situation arises when there is multiple path to access member of base class.
int main()
{
D object;
object.show();
}
Output:
Hello from A.
•abstract class in C++ is a class that has at least one pure virtual function (i.e., a function that has
no definition).
•The classes inheriting the abstract class must provide a definition for the pure virtual function;
otherwise, the subclass would become an abstract class itself.
•Sometimes implementation of all function cannot be provided in a base class because we don’t
know the implementation. Such a class is called abstract class.
•A pure virtual function (or abstract function) in C++ is a virtual function for which we can have
implementation, But we must override that function in the derived class, otherwise the derived class
will also become abstract class
•Friend Class A friend class can access private and protected members of other class in which it is declared as
friend.
• It is sometimes useful to allow a particular class to access private members of other class.
•Friend Function Like friend class, a friend function can be given a special grant to access private and
protected members. A friend function can be:
#include <iostream>
class A {
private:
int a;
public:
A() { a = 0; }
friend class B; // Friend Class
};
class B {
private:
int b;
public:
•Static data members in a class are shared by all the class objects as there is only one copy of them in
the memory, regardless of the number of objects of the class.
•Static methods in a class can only access static data members, other static methods or any methods
outside the class.
•We cannot use new keyword to create object with static class.
•We can access member of class using name of the class itself.
•A nested class is a member and as such has the same access rights as any other member.
•The member of an enclosing class have no special access to members of a nested class