0% found this document useful (0 votes)
16 views44 pages

20cs204 Class 4 Pp-II

Uploaded by

narendranvel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views44 pages

20cs204 Class 4 Pp-II

Uploaded by

narendranvel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

SRI RAMAKRISHNA ENGINEERING COLLEGE

VATTAMALAIPALAYAM, N.G.G.O. COLONY POST,


COIMBATORE – 641 022

20CS204
Programming Paradigm-II

CLASS 4
Date:11.08.2021
#include <iostream>
using namespace std; Fill in the blanks
void display ( ) int main()
{ {
int a = 3; display();
cout << a << endl; display(5);
} display(2.3);
void display (___) display(5,4.0);
{ return 0;
cout << a << endl; }

}
void ________ (double a ) Output
{
3
cout << a << endl;
5
}
2.3
void display(int a, ______ b)
{ 5,4
cout<< a << " , " << b << endl;
}
Constructors in C++

What is constructor?
A constructor is a member function of a class which initializes
objects of a class. In C++, Constructor is automatically called
when object(instance of class) create. It is special member
function of the class.
A constructor has the same name as that of the class and it
does not have a return type. For example,
class Wall {
public:

// create a constructor
Wall() {
// code
}
};
How constructors are different from a normal
member function?
A constructor has the same name as the class.
It doesn’t have any return type.
It is invoked whenever an object of its associated class is
created.
When a class is instantiated, even if we don’t declare a
constructor, compiler automatically creates one for the
program. This compiler created constructor is called default
constructor.
Declaration inside the class
class Class_name class A
{ {
public: public:
int variable; int variable;
class_name(){ // Declaring a constructor A() {
// BODY OF THE CLASS // BODY OF THE CLASS
} }
}; };
int main() int main()
{ {
Class_name c; // Creation of an object A c; // Creation of an object
return 0; return 0;
} }
Definition inside the class
class Class_name class A
{ {
public: public:
int variable; int variable;
class_name() // Defining a constructor A() // Defining a constructor
{ {
variable = 0; // Initialzing the object to 0 variable = 0; // Initialzing the object to 0
} }
}; };
int main() int main()
{ {
Class_name c; // Creation of an object A c; // Creation of an object
return 0; return 0;
}
}
Definition outside the class
class Class_name int main() class A int main()
{ { { {
Class_name c; A c;
public: public:
return 0; return 0;
int variable; } int variable; }
class_name(){ A(){
// Declaring a constructor // Declaring a constructor
// BODY OF THE CLASS // BODY OF THE CLASS
} }
}; };
//Defintion outside the class //Defintion outside the class
Class_name :: Class_name() A :: A()
{ {
variable = 0; variable = 0;
} }
Properties of Constructor in C++
The name of the constructor should be the same as that of the class.
A constructor should not have any return type.
A constructor is automatically invoked as soon as we create an
object.
A constructor is associated with the class.
A constructor may have default parameters or arguments.
We cannot use the const keyword along with the constructor name,
although C++ allows the constructor to be invoked for an object of
constant value.
A constructor does not have the provision to refer to its own address.
Types of Constructor in C++
Default Constructor
#include<iostream.h>
#include<string.h> void Display()
#include<conio.h> {
class Student
{ cout<<"\n\tRoll : "<<Roll;
int Roll;
char Name[25]; cout<<"\n\tName : "<<Name;
float Marks;
cout<<"\n\tMarks : "<<Marks;
public: }
};
Student() //Default Constructor void main()
{ {
Roll = 1; Student S; //Creating Object
strcpy(Name,"Kumar"); clrscr();
Marks = 78.42; S.Display(); //Displaying Student Details
getch();
} }
Parameterize Constructor
#include<iostream.h> void Display() Output :
#include<conio.h> {
#include<string.h> cout<<"\n\tRoll : "<<Roll; Roll : 2
Name : Sumit
class Student cout<<"\n\tName : "<<Name;
Marks : 89.63
{ cout<<"\n\tMarks : "<<Marks;
int Roll; }
char Name[25]; };
float Marks;
public: void main()
//Parameterize Constructor {
Student(int r,char nm[],float m) Student S(2,"Sumit",89.63);
{ //Creating Object and passing values to Constructor
Roll = r;
strcpy(Name,nm); S.Display();
Marks = m; //Displaying Student Details
}
}
Copy Constructor

Initialization of an object through another object is


called copy constructor. In other words, copying the
values of one object into another object is called copy
constructor

ClassName (const ClassName &old_obj);


Example 1
#include<iostream> int getX()
{
class Point return x;
{ } Output:
private: int getY() p1.x = 10, p1.y = 15
int x, y; { p2.x = 10, p2.y = 15
public: return y;
Point(int x1, int y1) }
{ };
x = x1; int main()
y = y1; {
} Point p1(10, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
// Copy constructor
Point(const Point &p2) // Let us access values assigned by constructors
{ cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
x = p2.x; cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
y = p2.y;
} return 0;
}
#include<iostream.h> Example 2
#include<conio.h>
#include<string.h>

class Student
{

int Roll;
char Name[25];
float Marks;

public:

Student(int r,char nm[],float m) //Constructor 1 : Parameterize Constructor


{
Roll = r;
strcpy(Name,nm);
Marks = m;
}
Student(Student &S) //Constructor 2 : Copy Constructor
{
Roll = S.Roll; void main()
strcpy(Name,S.Name); {
Marks = S.Marks;
} Student S1(2,"Sumit",89.63);

void Display() Student S2(S1); //Statement 1


{
cout<<"\n\tRoll : "<<Roll; cout<<"\n\tValues in object S1";
cout<<"\n\tName : "<<Name; S1.Display();
cout<<"\n\tMarks : "<<Marks;
} cout<<"\n\tValues in object S2";
}; S2.Display();
Output :
}
Values in object S1 Values in object S2
Roll : 2 Roll : 2
Name : Sumit Name : Sumit
Marks : 89.63 Marks : 89.63
Explanation of Copy Constructor
In the above example, Statement 1 is creating an object S2
and passing another object S1 as parameter to the constructor
2.
Constructor 2 will take the reference of object S1 passed by
the statement 1 and copy all the values of object S1 to data
members associated to the object S2.
Destructor in C++
We can use a destructor to destroy an object that has been
created by a constructor. In other words, destructors in C++ are
basically member functions that we use in order to destroy the
objects created by the constructor. Just like constructors, the
name of a destructor is same as that of a class. The name of a
destructor is preceded by the tilde sign ‘~’

Syntax
~ Class_name()
{
};
#include <iostream> Example
using namespace std;
class ABC
{
public:
ABC () //constructor defined
{ Output:
cout << "Hey look I am in constructor" << Hey look I am in constructor
endl; function main is terminating....
} Hey look I am in destructor
~ABC() //destructor defined
{
cout << "Hey look I am in destructor" << endl;
}
};
int main()
{
ABC cc1; //constructor is called
cout << "function main is terminating...." << endl;
/*....object cc1 goes out of scope ,now destructor is being called...*/
return 0;
} //end of program
Constructor Overloading in C++
In C++, We can have more than one constructor in a class with
same name, as long as each has a different list of
arguments.This concept is known as Constructor Overloading
and is quite similar to function overloading.
Overloaded constructors essentially have the same name
(name of the class) and different number of arguments.
A constructor is called depending upon the number and type
of arguments passed.
While creating the object, arguments must be passed to let
compiler know, which constructor needs to be called.
void display()
#include<iostream.h> Example {
#define PI 3.14
using namespace std; cout<<"The radius of the circle is: "<<radius<<endl;
class Circle cout<<"The circumference of the circle is:
{ "<<circumference<<endl<<endl;
float radius, circumference; }
public: };
Circle() // Definition of a Default constructor int main()
{ {
radius = 0; Circle c1(3);
} Circle c2(c1);
Circle(int r) // Definiton of a Parameterized constructor Circle c3;
{ c1.compute();
radius = r; c2.compute();
} c3.compute();
Circle(Circle &c) // Defintion of a Copy constructor c1.display();
{ c2.display();
radius = c.radius; c3.display();
} return 0;
void compute() }
{
circumference = 2 * PI * radius;
}
Binary Operator Overloading
The operators which operate on two operands or data are
called binary operators
Binary operator overloading is similar to unary operator
overloading except that a binary operator overloading requires
an additional parameter.
Binary Operators
Arithmetic operators (+, -, *, /, %)
Arithmetic assignment operators (+=, -=, *=, /=, %=)
Relational operators (>, <, >=, <=, !=, ==)
#include<iostream> Example void display()
using namespace std; {
class Complex cout << real << " + " << imaginary <<" i"<<endl;
{ }
private: };
int real, imaginary; int main()
public: {
Complex(int r = 0, int i =0) // Use of a constructor to cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;
initialize and store the values of both the variables Complex c1(3, 5), c2(2, 8);
{ Complex c3 = c1 + c2;
real = r; c3.display();
imaginary = i; return 0;
} }
Complex operator + (Complex const &c)
{
Complex result;
result.real = real + c.real;
result.imaginary = imaginary + c.imaginary;
return result;
}
Single Inheritance
This type of inheritance in C++ happens when the parent class
has only one child class. In other words, this is only one derived
class formed from a base class.
Syntax
class Base
{
// BODY OF THE BASE CLASS
};
class Derived : acess_specifier Base
{
// BODY OF THE DERIVED CLASS
};
Example1
#include<iostream.h> void salary()
#include<conio.h> {
cout<<"Enter employee salary: ";
cin>>e.salary; // access base class data member
class employee cout<<"Employee salary: "<<e.salary;
{ }
};
public:
int salary; void main()
{
};
clrscr();
class developer : public employee developer obj;
{ obj.salary();
getch();
employee e;
}
public:
Example 2
#include <iostream> class Derived : public Base
{
using namespace std; // private by default
class Base int derived_value;
{ public:
void derived_input()
public: {
int base_value; cout<<"Enter the integer value of derived class: ";
void base_input() cin>>derived_value;
}
{ void sum()
cout<<"Enter the integer value of base class: "; {
cin>>base_value; cout << "The sum of the two integer values is: " <<
base_value + derived_value<<endl;
}
}
}; };
Example 2
int main()
{
cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;
Derived d; // Object of the derived class
d.base_input();
d.derived_input();
d.sum();
return 0;
}
Multiple Inheritance
This type of inheritance happens when the child class inherits
its properties from more than one base class. In others, the
derived class inherits properties from multiple base classes.
Syntax
class A // Base class of B
{
// BODY OF THE CLASS A
};
class B // Derived class of A and Base class
{
// BODY OF THE CLASS B
};
class C : acess_specifier A, access_specifier A // Derived class of A and B
{
// BODY OF CLASS C
};
Example
#include<iostream> class B
using namespace std; {
public:
class A int B_value;
{ void B_input()
public: {
cout<<"Enter the integer value of class B: ";
int A_value; cin>>B_value;
void A_input() }
};
{
cout<<"Enter the integer value of class C : public A, public B //C is a derived class from
class A: "; classes A and B
{
cin>>A_value; public:
} void difference()
}; {
cout<<"The difference between the two values is: " <<
A_value - B_value<<endl;
}
};
Example 2
int main()
{
cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;
C c; // c is an Object of derived class C
c.A_input();
c.B_input();
c.difference();
return 0;
}
Hierarchical Inheritance
When multiple child classes inherit their properties from just a
single base class.
Syntax
class A // Base class of B
{
// BODY OF THE PROGRAM
};
class B : access_specifier A // Derived class of A
{
// BODY OF THE PROGRAM
};
class C : access_specifier A // Derived class of A
{
// BODY OF THE PROGRAM
};
class D : access_specifier A // Derived class of A
{
// BODY OF THE PROGRAM
};
Example
#include <iostream> class B : public A // B is derived from A
using namespace std; {
public:
class A
void product()
{ {
cout<<"The Product of the two values is: "<< x * y<<endl;
public:
}
int x, y; };
void A_input()
{
cout<<"Enter two values of class A: ";
cin>>x>>y;
}
};
Example
class C : public A //C is derived from A b.A_input();
{ b.product();
c.A_input();
public: c.division();
void division() return 0;
{ }

cout<<"The Division of the two values is: "<< x / y<<endl;


}
};
int main()
{
cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;
B b; // Object b of derived class B
C c; // Object c of derived class C
Multilevel Inheritance

This type of inheritance is the best way to represent the


transitive nature of inheritance. In multilevel inheritance, a
derived class inherits all its properties from a class that itself
inherits from another class.
Syntax
class A // Base class
{
// BODY OF CLASS A
};
class B : acess_specifier A // Derived class of A
{
// BODY OF CLASS B
};
class C : access_specifier B // Derived from derived class B
{
// BODY OF CLASS C
};
Example
#include <iostream>
using namespace std; class Derived1 : public Base // Derived class of base
class
class Base {
{ public:
public: int derived1_value;
void Derived1_input()
int base_value; {
void Base_input() cout<<"Enter the integer value of first derived class: ";
cin>>derived1_value;
{
}
cout<<"Enter the integer value of base class: "; };
cin>>base_value;
}
};
class Derived2 : public Derived1 // Derived class of Derived1 class
{
// private by deafult
int derived2_value;
public:
void Derived2_input()
{
cout<<"Enter the integer value of the second derived class: ";
cin>>derived2_value;
}
void sum()
{
cout << "The sum of the three intger values is: " << base_value + derived1_value +
derived2_value<<endl;
}
};
int main()
{
cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;
Derived2 d2; // Object d2 of second derived class
d2.Base_input();
d2.Derived1_input();
d2.Derived2_input();
d2.sum();
return 0;
}
Hybrid Inheritance
This type of inheritance essentially combines more than two
forms of inheritance. For instance, when a child class inherits
from multiple base classes all of its parent classes and that child
class itself serves as a base class for 3 of its derived classes.
Syntax
class A
{
// BODY OF THE CLASS A
};
class B : public A
{
// BODY OF THE CLASS A
};
class C
{
// BODY OF THE CLASS A
};
class D : public B, public C
{
// BODY OF THE CLASS A
};
Example
#include <iostream> class C
using namespace std; {
class A public:
{ int C_value;
public: C() //Use of a constructor to initialize C_value
int A_value; {
}; C_value = 40;
class B : public A }
{ };
public:
B() // Use of a constructor to initialize A_value
{
A_value = 20;
}
};
Example
class D : public B, public C // D is derived from class B and class C
{
public:
void product()
{
cout<<"The product of the two integer values is: " << A_value *
C_value<<endl;
}
};
int main()
{
cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;
D d; // Object d of derived class D
d.product();
return 0;
}

You might also like