20cs204 Class 4 Pp-II
20cs204 Class 4 Pp-II
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
class Student
{
int Roll;
char Name[25];
float Marks;
public:
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;
{ }