Introduction To C++-Module3-BPLCK105D-205D
Introduction To C++-Module3-BPLCK105D-205D
Constructor:-
1. Define Constructor with syntax, explain with an example?
Defn:- Constructor in C++ is a special method that is invoked automatically at the time of object
creation. It is used to initialize the data members of new objects generally.
o The constructor in C++ has the same name as the class or structure.
o Constructor is invoked at the time of object creation.
o Constructor is a member function of a class, whose name is same as the class name.
o Constructor is a special type of member function that is used to initialize the data members
for an object of a class automatically, when an object of the same class is created.
o Constructor is invoked at the time of object creation. It constructs the values i.e. provides
data for the object that is why it is known as constructor.
o Constructor do not return value, hence they do not have a return type.
Example:-- 1
// Example: defining the constructor within the class
#include<iostream>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
Example:-- 2
// Example: defining the constructor outside the class
#include<iostream>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student();
void display();
};
student::student()
{
cout<<"Enter the RollNo:";
cin>>rno;
cout<<"Enter the Name:";
cin>>name;
cout<<"Enter the Fee:";
cin>>fee;
}
void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
int main()
Example 2:
#include<iostream>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student() // Explicit Default constructor
{
cout<<"Enter the RollNo:";
cin>>rno;
cout<<"Enter the Name:";
cin>>name;
cout<<"Enter the Fee:";
cin>>fee;
}
void display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
};
int main()
{
student s;
2. Parameterized Constructors:
3. Copy Constructor:
A copy constructor is a member function that initializes an object using another object of the
same class. A detailed article on Copy Constructor.
Whenever we define one or more non-default constructors( with parameters ) for a class, a
default constructor( without parameters ) should also be explicitly defined as the compiler will not
provide a default constructor in this case. However, it is not necessary but it’s considered to be the
best practice to always define a default constructor.
Copy constructor takes a reference to an object of the same class as an argument.
Example 1:
#include<iostream.h>
using namespace std;
class Sample
{
int id;
public:
void init(int x)
{
id=x;
}
Sample(){} //default constructor with empty body
4. Constructor Overloading:
The use of multiple constructors in the same class is known as constructor overloading. The
constructor must follow one or both of the two rules below.
All the constructors in the class should have a different number of parameters.
It is also allowed in a class to have constructors with the same number of parameters and different
data types.
Examples of Legal and Illegal Constructor Overloading
Here are some examples of legal and illegal constructor overloading:
Addition(int x, int y) and Addition(double x, double y) is legal in constructor overloading.
Addition(int x, int y) and Addition(int x, double y) is legal in constructor overloading.
Addition(int x, int y) and Addition(int x, int y) is illegal in constructor overloading.
Addition(double x, double y) and Addition(double x, double y) is illegal in constructor
overloading.
Addition(int x, int y) and Addition(int x, int y, int z) is legal in constructor overloading.
Example: Structure of OverLoading
class ClassName {
public: ClassName() {
body; // Constructor with no parameter.
}
ClassName(int x, int y) {
body; // Constructor with two parameters.
}
ClassName(int x, int y, int z) {
body; // Constructor with three parameters.
}
ClassName(ClassName & object) {
body; // Constructor with the same class object as a parameter.
}};
Example 2:
// C++ program to demonstrate constructor overloading.
#include <iostream>
using namespace std;
class Area {
Default arguments of the constructor are those which are provided in the constructor
declaration. If the values are not provided when calling the constructor the constructor uses the
default arguments automatically.
Example:
#include<iostream>
using namespace std;
class Simple{
int data1;
int data2;
int data3;
Explanation of code:
1st we created a “simple” class which consists of private data members “data1”, “data2” and
“data3”.
2nd parameterized constructor of the “simple” class is defined which takes three parameters
and assigns values to the data members “a” and “b”. The main thing to note here is that the
value “9” and “8” are the default values for the variables “b” and “c”.
3rd function “printData” is defined which prints the values of the data members “data1”,
“data2”, and “data3”.
4th parameterized constructor is called with the object “s” of the data type “simple” and the
values “12” and “13” are passed.
The main thing to note here is that the value of the parameter “c” will be automatically set by
the default value.
2nd function “printData” is called which will print the values of data members.
Destructor:-
Defn:- A destructor is also a special member function as a constructor. Destructor destroys the
class objects created by the constructor.
Destructor has the same name as their class name preceded by a tilde (~) symbol.
The syntax for defining the destructor within the class
~ <class-name>()
{
}
The syntax for defining the destructor outside the class
<class-name>: : ~ <class-name>(){}
Destructor has the same name as their class name preceded by a tilde (~) symbol.
It is not possible to define more than one destructor.
The destructor is only one way to destroy the object created by the constructor. Hence destructor
can-not be overloaded.
Destructor neither requires any argument nor returns any value. It is automatically called when
the object goes out of scope.
Example1:-
#include <iostream>
using namespace std;
class Test {
public:
Test() { cout << "\n Constructor executed"; }
Example2:-
#include <iostream>
using namespace std;
int count = 0;
class Test {
public:
Test()
{
count++;
cout << "\n No. of Object created:\t" << count;
}
~Test()
{
cout << "\n No. of Object destroyed:\t" << count;
--count;
}
};
main()
{
Test t, t1, t2, t3;
return 0;
}
Properties of Destructor:-
1. Destructor is invoked automatically by the compiler when its corresponding constructor goes out
of scope and releases the memory space that is no longer required by the program.
2. Destructor neither requires any argument nor returns any value therefore it cannot be overloaded.
3. Destructor cannot be declared as static and const;
4. Destructor should be declared in the public section of the program.
5. Destructor is called in the reverse order of its constructor invocation.
Inheritance:
Defn:- The capability of a class to derive properties and characteristics from another class is called
Inheritance.
Inheritance is one of the most important features of Object-Oriented Programming.
Inheritance is a feature or a process in which, new classes are created from the existing classes.
The new class created is called “derived class” or “child class” and the existing class is known as
the “base class” or “parent class”. The derived class now is said to be inherited from the base
class.
When we say derived class inherits the base class, it means, the derived class inherits all the
properties of the base class, without changing the properties of base class and may add new
features to its own. These new features in the derived class will not affect the base class. The
derived class is the specialized class for the base class.
class A {
public:
int x;
protected:
int y;
private:
int z;
};
class B : public A {
// x is public
// y is protected
// z is not accessible from B
};
class C : protected A {
// x is protected
// y is protected
// z is not accessible from C
};
class D : private A // 'private' is default for classes
{
// x is private
// y is private
// z is not accessible from D
};
Types of Inheritance:-
Single inheritance
Multilevel inheritance
Multiple inheritance
Hierarchical inheritance
Hybrid inheritance
1. Single Inheritance:
In single inheritance, a class is allowed to inherit from only one class. i.e. one subclass is inherited by
one base class only.
// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
};
// main function
2. Multiple Inheritance:
Multiple Inheritance is a feature of C++ where a class can inherit from more than one class. i.e one
subclass is inherited from more than one base class.
Syntax:
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
Output
This is a Vehicle
This is a 4 wheeler Vehicle
Syntax:-
class C
{
... .. ...
};
class B:public C
{
... .. ...
};
class A: public B
{
... ... ...
};
Example:-
C++ program to implement
// Multilevel Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
Output
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
4. Hierarchical Inheritance:
In this type of inheritance, more than one subclass is inherited from a single base class. i.e. more than
one derived class is created from a single base class.
Syntax:-
class A
{
// body of the class A.
}
class B : public A
Example:-
// C++ program to implement
// Hierarchical Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}
Output
This is a Vehicle
This is a Vehicle
Example:-
C++ program for Hybrid Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// base class
class A{
public:
Fare() { cout << "In A Class\n"; }
};
// first sub class
class Car : public Vehicle {
};
// second sub class
class Bus : public Vehicle, public A {
};
// main function
int main()
{
Output
This is a Vehicle
In A Class.