OOP Lab 05
OOP Lab 05
Lab 05
C++ Constructors
Example-1
#include <iostream>
using namespace std;
class student {
int rno;
char name[10];
double fee;
public:
student()
{
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; // constructor gets called automatically when
// we create the object of the class
s.display();
return 0;
}
OUPUT
Example-2
#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;
int main()
{
student s;
s.display();
return 0;
}
OUPUT
Example-3
#include <iostream>
using namespace std;
class Line {
public:
void setLength( double len );
double getLength( void );
Line( double len ); //This is the constructor
private:
double length;
};
//Member function definition including constructor
Line::Line( double len ) {
cout<<"Object is being created , length ="<< len <<endl;
length = len;
}
void Line::setLength( double len ) {
length = len;
}
double Line::getLength( void ) {
return length;
}
//Main function for the program
int main() {
Line line(10.0);
//get initially set length
cout<<"Length of line :" << line.getLength() << endl;
//set line length again
line.setLength(6.0);
cout<<"Length of line :" << line.getLength() << endl;
return 0;
}
#include <iostream>
using namespace std;
class construct {
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: " << c.a << endl << "b: " << c.b;
return 1;
}
Note: Even if we do not define any constructor explicitly, the compiler will
automatically provide a default constructor implicitly.
Example
// CPP program to illustrate parameterized constructors
#include <iostream>
using namespace std;
class Point {
private:
int x, y;
public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}
int main()
{
// Constructor called
Point p1(10, 15);
return 0;
}
OUPUT
Example
#include<iostream>
#include<string.h>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student(int,char[],double);
void display();
};
void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
int main()
{
student s(10,"Ali",1000);
s.display();
return 0;
}
OUPUT
Sample(Sample &t)
{
id=t.id;
}
#include<iostream>
using namespace std;
class Sample
{ int id;
public:
void init(int x)
{
id=x;
}
void display()
{
cout<<endl<<"ID="<<id;
}
};
int main()
{
Sample obj1;
obj1.init(10);
obj1.display();
};
void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
int main()
{
student s(1001,"Manjeet",10000);
s.display();
return 0;
}
Constructor overloading
Example of constructor overloading with a different number of parameters and constructors with the
same name.
Output
Sum of two numbers is =
7
the sum of three numbers is =
12
Example of constructors with the same name and have the same number of
parameters but of different data types.
Output
Sum of two numbers is =
7
the sum of three numbers is =
10
Example of Constructor Overloading In C++
Output
I am Constructor
Im Constructor
Values :33 +7=40
Values :3 +7=10
Q1: Write a program in C++ to convert a decimal number into binary without using an array and
using the constructor.
Q2: Write a C++ Program To calculate volume of Box using parametrized Constructor
Q3: Write a program in C++ to convert a decimal number into binary without using an
array by using the constructor overloading.
Q4: Write a program in C++ to convert a decimal number into binary without using an array
by using the constructor overloading.
Q5: Write a C++ Program to show Default copy constructor as describe in the following
output.