You are on page 1of 7

CSE 251

SUBMITTED TO SUBMITTED BY

BABITA PANDEY NAVDEEP SINGH

B.TECH(H)CSE MBA

RA6903A31

REG NO.-7450070084

PART-A
Q1. Describe different types of access specifiers in C++.

Ans- Access specifiers:


Private
Public
protected

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. 
.

Public  members are accessible from outside the class. 

A protected access specifier is a stage between private and public access. If member


functions defined in a class are protected, they cannot be accessed from outside the class but
can be accessed from the derived class.

Q2.What is the difference between class and structures?


Ans- Structure: Initially (in C) a structure was used to bundle different type of data types
together to perform a particular functionality. But C++ extended the structure to contain
functions also. The major difference is that all declarations inside a structure are by default
public.

 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.

we CAN access the members without inheriting the class.

DEMERITS-

disadvantage of friend functions is that they require an extra line 


of code when you want dynamic binding. To get the effect of a virtual friend, 
the friend function should call a hidden function.

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-

What is the use of Constructor


The main use of constructors is to initialize objects. The function of
initialization is automatically carried out by the use of a special member
function called a constructor.

What is the use of Destructors


Destructors are also special member functions used in C++ programming
language. Destructors have the opposite function of a constructor. The main
use of destructors is to release dynamic allocated memory. Destructors are
used to free memory, release resources and to perform other clean up.
Destructors are automatically named when an object is destroyed. Like
constructors, destructors also take the same name as that of the class
name.

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();
}

You might also like