You are on page 1of 16

Q.How a class can be separated from its implementation?

Explain
Ans:The specification of a class can be separated from its implementation by placing the
class declaration in a header file. The definitions of a member function are stored in .cpp
files of their own. The header file which contains the declaration of the class is called class
specification file and it has .h extension. The .cpp file which stores the member function
definitions is called class implementation file. The class header file must be preceded with
#include and .cpp file must be compiled and linked only with the main program.
Square.h
#ifndef SQUAR_H
#define SQUARE_H
class square
{
private:
double side;
public:
void setSide(double);
double getSide() const;
double getArea() const;
};
Square.cpp
#include "square.h"
#include<iostream.h>
#include<cstdlib.h>
using namespace std;
void square:: setSide(double S);
{
if(S>=0)
side=S;
else
{
cout<"Invalid side\n";
exit(EXIT_FAILURE);
}
}
double square::getSide() const
{
return side;
}
double square::getArea() const
{
return side*side;
}

Square_main.cpp
#include<iostream.h>
#include "square.h"
using namespace std;
int main()
{
square box;
double sqSide;
cout<<"This program will calculate the area of a square\n";
cout<<"What is the side of the square?\n”
cin>>sqSide;
box.setSide(sqSide);
cout<<"Here is the squares data:\n";
cout<<"Side:"<<box.getSide()<<endl;
cout<<"Area:"<<box.getArea()<<endl;
return 0;
}
Q.Define constructor. Explain various types of constructors in C++.
Ans: Constructor

A constructor is a member function which automatically gets called whenever the object of
the class is instantiated. It does not return any value.
Syntax:
class integer
{
int m,n;
public:
integer(void);
}
integer::integer(void)
{
m=0;
n=0;
}
Types of constructors
1. Default constructor
2. Parameterized constructor
3. Copy constructor
4. Constructor with default arguments
Default Constructor
A constructor that does not take any argument is known as default constructor. If no
constructor is specified then a default constructor is called automatically. It does not accept
any argument.
Program
#include<iostream.h>
#include<conio.h>
class number
{
private:
int x;
float y;
public:
number();
void display()
{
cout<<"\n\t x="<<x<<"\t y="<<y;
}
};
number::number()
{
cout<<"\n Constructor containing no arguments:";
x=y=NULL;
}
int main()
{
class number C;
C.display();
getch();
return 0;
}
Parameterized constructor
A constructor that takes arguments as its parameters is known as parameterized
constructor. It is used to initialize the elements that take different values. Whenever objects
are declared then their initial values are passed as arguments to constructor function. This
can be done using following two methord
(i) Explicit constructor calling
(ii) Implicit constructor calling
(a)Explicit constructor calling
In this method, the constructor is called explicitly by means of writing its name and passing
arguments ( if any).
Syntax
class_name object_name=constructor_name(arg1, arg2);
eg: point obj1=point(10,15)
(b)Implicit constructor calling
In this method, the constructor is called implicitly by means of simply writing the class name
followed by object name and passing parameters without using the name of the
constructor.
Syntax
class_name object_name(arg1,arg2);
eg: point obj1(10,15)
Program
#include<iostream.h>
#include<conio.h>
class mylcass
{
int x,y;
public:
myclass(int p, int q)
{
x=p;3
y=q;5
}
void show()
{
cout<<”x=”<<x<<""<<”y=”<<y;
}
};
int main()
{
myclass mc(3,5);
mc.show();
getch();
return 0;
}
Output
X=3 y=5
Copy constructor
Copy constructor is a constructor function used for copying objects. It has the same name as
that of a class and takes a reference to a constant parameter.
This constructor copies one object from the other sequentially during the object
declaration. This process of initializing is called copy initialization.
Syntax:
Class A
{
………..
………..
Public:
A(A &c);

Program
#include<iostream.h>
#include<conio.h>
class Example
{
private:
int data;
public:
Example()
{
}
Example(int a)
{
data=a;
}
Example(Example &c)
{
data=c.data;
cout<<"Copy constructor invoked";
}
void show()
{
cout<<data;
}
};
main()
{
clrscr();
Example e(20);
e.show();
Example e1;
e1=e;
e1.show();
getch();
return 0;
}
Output
20
20
Constructors with default arguments
C++ allows the programmer to invoke a constructor without specifying all of its arguments.
In such situation, the constructor assigns a default value to the argument that doesn’t have
a matching value. The assignment of default value is done during function declaration.
Generally, a default argument is checked for type during its declaration and evaluated at the
time of its declaration.
Program
#include<conio.h>
#include<iostream.h>
class student
{
int rollno;
float marks;
public:
student(int p=10, float q=40)
{
rollno=p;10
marks=q;40
}
void show()
{
cout<<"Roll no"<<rollno<<endl;
cout<<"Marks"<<marks<<endl;
}
};
void main()
{
student x;
student y(1,50);
clrscr();
cout<<"Output using default constructor arguments:\n";
x.show();
cout<<"Output without default constructor arguments:\n";
y.show();
getch();
}

Write briefly about destructor.


Destructor
A destructor is a special member function that is used to destroy the objects created by
constructor. It takes the same name as the class but with a ‘tild’(~). It doesn’t have any
return type. It is called automatically at the end of program execution to free up the
acquired shortage.
Memory allocation is done by using new operator in a constructor whereas delete operator
is used in a destructor to deallocate the previously allocated memory.
Program
#include<iostream.h>
#include<conio.h>
class Demo
{
private:
int data;
public:
Demo()
{
cout<<endl<<"Inside Constructor";
}
~Demo()
{
cout<<endl<<"Inside destructor";
}
};
void main()
{
Demo d;
getch();
}
Q.Can a constructor be overloaded? Justify
Ans:Yes, a constructor can be overloaded. A class can have multiple constructors.
Constructors are said to be overloaded if all of them have the same name as class name but
differ in signatures. The signatures of a constructor includes the number of arguments and
data type of these arguments. An object is allowed to be created with different parameter
list. When an object is created, a constructor is invoked based on the number and types of
arguments passed. The following program illustrates constructor overloading.
Write a program to find area of rectangle, cube and square using constructors.
#include<iosream.h>
Using namespace std;
class construct
{
public:
float area;
construct(int l, int b);
{
area=l*b;
}
construct(double r)
{
area=3.4*r*r;
}
construct (int s)
{
area=s*s;
}
void display()
{
cout<<area<<endl;
}
};
int main()
{
construct obj1(5,10);
construct obj2(3.5);
construct obj3(5);
cout<<"Area of rectangle is";
obj1.display();
cout<<"\n Area of circle is:";
obj2.display();
cout<<"\n Area of square is : ";
obj3.display();
return 1;
}
Output
Area of rectangle is 50
Area of circle is 38.465
Area of square is 25
Q.Explain about creating and accessing array of objects with example programs
Ans:
Program:
Output:
Q. Explain instance of a class with proper examples.
Ans: Instance member of a class
A class contains several objects that are considered as instances of a class .every object
maintains a copy of a class member variables for itself. similarly even other objects maintain
Copies of the class member variables. The member variables of an object are different from
the member variables of the other object of the same class .
Consider a class square that has two objects S1 and S2 defined for it
Let the side of the square set as shown below
S1.setside(15);
S2.setside(20);
Here S1 and S2 are two different objects having its own dimension called side

You might also like