Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Assignment

OBJECT ORIENTED PROGRAMMING


OOP
(SE-104)

Submitted by
Zaryab Dar
(23014198-153)
2nd Semester
SE-23-C
Question no 1:
Explain the concept of a default constructor in C++. Provide a code example
demonstrating the use of a default constructor in a class.

Answer:
Constructors are special member functions. A constructor without any arguments or
with the default value for every argument is said to be the Default constructor. They
share the same name as the class and have no return type.
A constructor that has zero parameter list or in other sense, a constructor that accept
no arguments is called a zero-argument constructor or default constructor.
If default constructor is not defined in the source code by the programmer, then the
compiler defined the default constructor implicitly during compilation.
If the default constructor is defined explicitly in the program by the programmer, then
the compiler will not define the constructor implicitly, but it calls the constructor
implicitly.
Syntax:
class classname {
public:
//Declaring the default constructor.
classname() {
}
};

Example:
#include <iostream>
using namespace std;
class cars
{
private:
string modelno;
string company;
long long int price;
public:
cars()
{
modelno="SV30-0169266";
company="Honda";
price=10000000.000;
}
void display();
};
void cars::display()
{
cout<<modelno<<endl;
cout<<company<<endl;
cout<<price<<endl;
}
int main()
{
cars civic;
civic.display();
return 0;
}

Output:

Question no 2:
Illustrate the use of a parameterized constructor in C++. Provide a class definition with
a parameterized constructor and demonstrate its usage with an object instantiation.

Answer:
The parameterized constructors are the constructors having a specific number of
arguments to be passed. The purpose of a parameterized constructor is to assign user-
wanted specific values to the instance variables of different objects. They are
categorized on the bases of number of arguments passing to the constructor:

 One Argument Constructor


 Two Argument Constructor
 Three Argument Constructor.......etc
Example:
#include<iostream>
using namespace std;

class studentresult
{
private:
int totalmarks;
char grade;
string promotion;
public:
studentresult(int tm,char G,string P)
{
totalmarks=tm;
grade=G;
promotion=P;
}
void display()
{
cout<<"Total marks "<<totalmarks<<endl;
cout<<"Grade "<<grade<<endl;
cout<<"Promotion "<<promotion<<endl;
}
};
int main()
{
studentresult zaryab(300,'C',"Yes");
cout<<"Result of zaryab is"<<endl;
zaryab.display();
return 0;
}

Output:

Question no 3:
Discuss the concept of a copy constructor in C++ and provide a code snippet
demonstrating its implementation within a class.
Answer:
A copy constructor is a member function that initializes an object using another object
of the same class. In simple terms, a constructor which creates an object by initializing
it with an object of the same class, which has been created previously is known as a
copy constructor.

Example:
include <iostream>
using namespace std;
class marker
{
private:
int markerno;
string company;
double price;

public:
marker(int m, string comp, double p)
{
markerno = m;
company = comp;
price = p;
}
void displaydata()
{
cout<<markerno<<endl;
cout<<company<<endl;
cout<<price<<endl;
}
};
int main()
{
marker m1(101,"Dollar",51.50);
marker m2=m1;
m2.displaydata();
return 0;
}

Output:
Question no 4:
Explain the concept of constructor overloading in C++ with an example. Provide a class
with multiple constructors demonstrating different parameter types.

Answer:
Constructors can be overloaded in a similar way as function overloading.
Overloaded constructors have the same name (name of the class) but the different number of
arguments. Depending upon the number and type of arguments passed, the corresponding
constructor is called.

Example:
#include<iostream>
using namespace std;

class Room
{
private:
float length;
double breadth;
public:
Room(float L)
{
length=L;
breadth=4.2;
}
Room(float L,double B)
{
length=L;
breadth=B;
}
double Area()
{
return length*breadth;
}
};
int main()
{
Room R1(7.9),R2(4.6,5.4);
cout<<"Area of room1 is "<<R1.Area()<<endl;
cout<<"Area of room2 is "<<R2.Area()<<endl;
return 0;
}

Output:

You might also like