Professional Documents
Culture Documents
SUBMITTED TO SUBMITTED BY
B.TECH(H)CSE MBA
RA6903A31
REG NO.-7450070084
PART-A
Q1. Describe different types of access specifiers in C++.
A private member within a class denotes that only members of the same class have
accessibility. The private member is inaccessible from outside the class.
.
Class: Class is a successor of Structure. By default all the members inside the class are private.
Q3. Describe the need for friend function? What are the merits and demerits of
using friend function?
Ans-
NEED- As we know private members cannot be accessed from outside the class, i.e a non-
member function cannot have an access to private data of class, or there could be a situation in
which we would like two classes to share a particular function- in such situations c++ allows the
common fuctions to be made friendly with both classes, this all is done by a friend function.
MERITS-
we can able to access the other class members in our class if we use friend keyword.
DEMERITS-
PART-B
Q4. Can multiple constructors function in a class are feasible? Write a program to
implement the concept.
Ans-yes, c++ permits multiple constructors in the same class.
Example-
public clsRect ()
{
Console.WriteLine ("Default constructor called");
m_Left = m_Top = m_Right = m_Bottom = 0;
}
public clsRect (int cx1, int cy1, int cx2, int cy2)
{
Console.WriteLine ("Constructor 1 called");
m_Left = cx1;
m_Top = cy1;
m_Right = cx2;
m_Bottom = cy2;
}
public clsRect (POINT pt1, POINT pt2)
{
Console.WriteLine ("Constructor 2 called");
m_Left = pt1.cx;
m_Top = pt1.cy;
m_Right = pt2.cx;
m_Bottom = pt2.cy;
}
public POINT UpperLeft
{
get {return(new POINT(m_Left, m_Top));}
set {m_Left = value.cx; m_Top = value.cy;}
}
public POINT LowerRight
{
get {return(new POINT(m_Right, m_Bottom));}
set {m_Right = value.cx; m_Bottom = value.cy;}
}
private int m_Left;
private int m_Top;
private int m_Right;
private int m_Bottom;
}
}
Q5. Give the need for constructors and destructors. What is parameterized
constructor? Explain with the help of program.
Ans-
PARAMETERIZED CONSTRUCTORS
The constructors that can take arguments are called parameterized constructors i.e objective is
achieved by passing arguments to the constructor function when the objects are created.
Example-
#include <iostream>
using namespace std;
class myclass {
int a, b;
public:
myclass(int i, int j) {
a=i;
b=j;
}
void show() {
cout << a << " " << b;
}
};
int main()
{
myclass ob(3, 5);
ob.show();
return 0;
}
Q6. Create two classes DM and DB which store the value of distances. DM stores distance in
meters and centimeters and DB in feet and inches. Write a program that can read values for the
class objects and add one object of DM with another object of DB. Use a friend function to carry
out the addition operation. The object that stores may be DM object or DB object, depending on
the units in which the result are required.
Ans-
#include<iostream.h>
#include<conio.h>
class DB;
class DM
{
// public:
int m,c;
public:
DM()
{
cout<<”\n plz enter the meter:-”;
cin>>m;
cout<<”\n plz enter the Centimeter:-”;
cin>>c;
}
void putdata()
{
cout<<”\n meter =”<<m<<endl<<”Centimeter”<<c;
}
//friend void add(DM,DB);
};
class DB
{
int f,i;
public:
DB()
{
cout<<”\n plz enter the feet:-”;
cin>>f;
cout<<”\n plz enter the inches:-”;
cin>>i;
}
void putdata()
{
cout<<”\n feet =”<<f<<endl<<”inches”<<i;
}
// friend void add(DM,DB);
};
/*
void add(DM m1,DB b1)
{
int a,b;
a=b1.i+m1.c;
b=b1.f+m1.m;
cout<<”\n inches + centimeter :-”<<a;
cout<<”\n meter + feet :-”<<b;
}
*/
void main()
{
clrscr();
DM m1;
DB b1;
m1.putdata();
b1.putdata();
// add(m1,b1);
getch();
}