Unit 3 Full PPT Content
Unit 3 Full PPT Content
Unit 3 :
02 Syntax
1. Code Reusability
04 Advantages
2. Method Overriding (Hence, Runtime Polymorphism.)
3. Use of Virtual Keyword
Inheritance Types
01 Single 02 Multiple 03 Hierarchical
04 Multilevel
05 Hybrid
Modes of
Inheritance
If we derive a sub class from a public base class. Then the public
01 Public member of the base class will become public in the derived class and
protected members of the base class will become protected in derived
class
If we derive a sub class from a Protected base class. Then both public
02 Protected
member and protected members of the base class will become
protected in derived class.
If we derive a sub class from a Private base class. Then both public
03 private member and protected members of the base class will become
Private in derived class.
Syntax:
class Classname // base class
{
..........
};
class classname: access_specifier baseclassname
{
…
};
Example
void product()
#include <iostream> {
using namespace std; cout << "Product = " << x * y;
class base //single base class }
{ public: };
int x;
void getdata() int main()
{ {
cout << "Enter the value of x = "; derived a; //object of derived class
cin >> x;
} a.getdata();
};
class derived : public base //single derived a.readdata();
{
int y; a.product();
public:
void readdata() return 0;
{ }
cout << "Enter the value of y = ";
cin >> y;
}
Applications of Single Inheritance
Student
Multiple Inheritance
Syntax:
class A // base class
{
..........
};
class B
{
..........
}
class c : access_specifier A, access_specifier B // derived class
{
...........
};
Example:
Distributed Database
Multilevel Inheritance
A derived class can be derived from another derived class. A child class
can be the parent of another class.
Syntax:
class A // base class
{
..........
};
class B
{
..........
}
class C : access_specifier B
// derived class
{
...........
};
car()
// base class {
class Vehicle cout<<"Car has 4 Wheels”;
{ }
public: };
Vehicle() // main function
{ int main()
cout << "This is a Vehicle"; {
} //creating object of sub class will
}; //invoke the constructor of base classes
class fourWheeler: public Vehicle Car obj;
{ public: return 0;
fourWheeler() }
{
cout<<"Objects with 4 wheels are
vehicles"<<endl;
}
};
// sub class derived from two base classes
class Car: public fourWheeler{
public:
Hierarichal Inheritance
• In Hierarichal Inheritance we
have several classes that are
derived from a common base
class (or parent class).
• Here in the diagram Class 1,
Class 2 and Class 3 are derived
from a common parent class
called Base Class.
Hierarchical Inheritance
How to implement Hierarchal Inheritance in C++
class A {
// Body of Class A • In the example present in the
}; // Base Class
left we have class A as a parent
class and class B and class C
class B : access_specifier A
that inherits some property of
{
Class A.
// Body of Class B
}; // Derived Class
• While performing inheritance it
class C : access_specifier A is necessary to specify the
{ access_specifier which can be
// Body of Class C public, private or protected.
}; // Derived Class
Example
#include <iostream> class C : public A //C is also derived from
using namespace std; class base
class A //single base class {
{ public:
public: void sum()
int x, y; {
void getdata() cout << "\nSum= " << x + y;
{ }
cout << "\nEnter value of x and y:\n"; };
cin >> x >> y; int main()
} {
}; B obj1; //object of derived class B
class B : public A //B is derived from class base C obj2; //object of derived class C
{ obj1.getdata();
public: obj1.product();
void product() obj2.getdata();
{ obj2.sum();
cout << "\nProduct= " << x * y; return 0;
} }
};
Example of Hierarchical Inheritance
class C
{
// Class C body
};
Access Specifiers
In C++ we have basically three types of access specifiers :
• Public : Here members of the class are accessible outside the class as
well.
• Private : Here members of the class are not accessible outside the
class.
• Protected : Here the members cannot be accessed outside the class,
but can be accessed in inherited classes.
Example of Hybrid Inheritance
class A class C
{ { public:
public: int y;
int x;
C()
};
{
class B : public A y = 4;
{ }
public: };
B()
class D : public B, public C
{
x = 10; { public:
} void sum()
}; {
cout << "Sum= " << x + y;
Order of Constructor Call
Base class constructors are always called in the derived class constructors.
Whenever you create derived class object, first the base class default constructor is
executed and then the derived class's constructor finishes execution.
Points to Remember
Note:
where friend is a keyword used as a function
modifier. A friend declaration is valid only
03 within or outside the class definition.
Friend Function
Syntax: Case 2:
class second; forward declaration
class first class sample
{ {
private: private:
-------------- int x;
public: float y;
friend return_type public:
fname(first one, second two); virtual void display();
}; virtual static int sum(); //error
class second }
{ int sample::sum()
private: {}
------------------
public:
friend return_type
fname(first one, second two);
};
Friend Function Example
class sample Example: {
{ clrscr();
private:
int x; sample obj;
public:
void getdata(); obj.getdata();
friend void display(sample abc); cout<<"Accessing the private data by non -
}; member function"<<endl;
void sample::getdata() display(obj);
{
cout<<"Enter a value for x\n"<<endl; getch();
cin>>x; }*/
}
void display(sample abc)
{
cout<<"Entered Number is "<<abc.x<<endl;
}
void main()
Friend Function Example
class first Example:
{ void second::disp(first temp)
friend class second; {
private: cout<<"Entered Number is = "<<temp.x<<endl;
int x; }
public:
void getdata(); void main()
}; {
void first::getdata()
{
cout<<"Enter a Number ?"<<endl;
cin>>x;
}
Friend Function Example
class second; //Forwardvoid first::getdata() int temp;
Declaration { temp = one.x + two.y;
class first cout<<"Enter a Value for X"<<endl; return(temp);
{ cin>>x; }
private: } void main()
int x; void second::getdata() {
public: { first a;
void getdata(); cout<<"Enter a value for Y"<<endl; second b;
void display(); cin>>y; a.getdata();
friend int sum(first one,second two); } b.getdata();
}; void first::display() a.display();
class second { b.display();
{ cout<<"Entered Number is X = "; int te = sum(a,b);
private: } cout<<"Sum of the two
int y; void second::display() Private data variable (X + Y)";
public: { cout<<" = "<<te<<endl;
void getdata(); cout<<"Entered Number is Y = ";
void display(); } }
friend int sum(first one,second two); int sum (first one,second two)
}; {
Inline Member Function
Inline functions are used in C++ to reduce the overhead of a normal function call.
A member function that is both declared and defined in the class member list is called an inline member function.
The inline specifier is a hint to the compiler that inline substitution of the function body is to be preferred to the
usual function call implementation.
3) If a function is recursive.
4) If a function return type is other than void, and the return statement doesn’t
exist in function body.
#include <iostream>
using namespace std;
inline int cube(int s)
{
return s*s*s;
}
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
}
All the functions defined inside the class are implicitly inline. Thus, all the restrictions of inline
functions are also applied here.
If you need to explicitly declare inline function in the class then just declare the function inside
the class and define it outside the class using inline keyword
#include <iostream>
using namespace std;
class operation inline void operation :: sum()
{ {
int a,b,add; add = a+b;
cout << "Addition of two numbers: " << a+b << "\n";
public: }
void get();
void sum(); int main()
}; {
inline void operation :: cout << "Program using inline function\n";
get() operation s;
{ s.get();
cout << "Enter first s.sum();
value:"; return 0;
cin >> a; }
cout << "Enter second
value:";
cin >> b;
}
t:
st value: 45 Enter second value: 15 Addition of two numbers: 60 Difference of two numbers: 30 Product of two numbers: 675 Division of two numbers: 3
Virtual function
• Virtual Function is a function in base class, which is
overridden in the derived class, and which tells the Syntax
compiler to perform Late Binding on this function.
01 virtual return_type function_name (arg);
• Virtual Keyword is used to make a member function of
the base class Virtual. Virtual functions allow the most Example
specific version of a member function in an inheritance virtual void show()
hierarchy to be selected for execution. Virtual functions 02 {
make polymorphism possible. cout << "Base class\n";
Key: }
• Only the Base class Method's declaration needs the
Note:
Virtual Keyword, not the definition.
We can call private function of derived class
• If a function is declared as virtual in the base class, it from the base class pointer with the help of
will be virtual in all its derived classes. 03 virtual keyword. Compiler checks for access
specifier only at compile time. So at run time
when late binding occurs it does not check
whether we are calling the private function or
public function.
Virtual function features
Case 1: Case 2:
However, a flowchart on the other hand portrays the processes or commands that
on execution change the state of class or an object of the class.
When to use State charts
So the main usages can be described as:
To model object states of a system.
To model reactive system. Reactive system consists of reactive
objects.
To identify events responsible for state changes.
Forward and reverse engineering.
How to draw state charts
Before drawing a State chart diagram we must have clarified the following points:
Identify important objects to be analysed.
Identify the states.
Identify the events.
Elements of state chart diagrams
• Initial State: This shows the starting point of the state chart diagram that is where
the activity starts.
Elements of state chart diagrams
• State: A state represents a condition of a modelled entity for which some action is
performed. The state is indicated by using a rectangle with rounded corners and
contains compartments
Elements of state chart diagrams
• Composite state – We use a rounded rectangle to represent a
composite state also. We represent a state with internal activities
using a composite state.
Elements of state chart diagrams
• Fork – We use a rounded solid rectangular bar to represent a Fork notation with
incoming arrow from the parent state and outgoing arrows towards the newly
created states. We use the fork notation to represent a state splitting into two or
more concurrent states.
Elements of state chart diagrams
• Join – We use a rounded solid rectangular bar to represent a Join notation with
incoming arrows from the joining states and outgoing arrow towards the common
goal state. We use the join notation when two or more states concurrently
converge into one on the occurrence of an event or events.
Elements of state chart diagrams
• Transition: It is indicated by an arrow. Transition is a relationship between two
states which indicates that Event/ Action an object in the first state will enter the
second state and performs certain specified actions.
Elements of state chart diagrams
• Transition – We use a solid arrow to represent the transition or change of control
from one state to another. The arrow is labelled with the event which causes the
change in state.
Elements of state chart diagrams
• Self transition – We use a solid arrow pointing back to the state itself to represent
a self transition. There might be scenarios when the state of the object does not
change upon the occurrence of an event. We use self transitions to represent such
cases.
Elements of state chart diagrams
• Final State: The end of the state chart diagram is represented by a solid circle
surrounded by a circle.
Example state chart for ATM card PIN Verification
•
Example state chart for order management system
ACTIVITY DIAGRAM