Вы находитесь на странице: 1из 4

Maharaja Agrasen Model School, CD-Block Pitam Pura,Delhi-34.

Practice Assignment – Classes & Objects , Constructor and Destructor


1. What do you understand by Data Encapsulation? How it gets
implemented in C++?
2. What is difference between Private and Public Data members of a class?
3. What is the use of a constructor function in a class ? Give a suitable
example for a constructor function in a class.
4. Differentiate between a constructor and destructor function.
5. What do you understand about a member function ? how does a member
function differ from an ordinary function ?
6. What do you understand about a static member function? How does a
static member function differ from a member function?
7. Can you give a structure definition with in a class?
8. Differentiate between default & copy constructor with a suitable
example.
9. Differentiate between a date type struct & a data type class in C++.
10. Explain the concept constructor overloading with a suitable example.
11. Why the object is passed by reference in case of copy constructor?
12. Why is a destructor function required in classes? Illustrate with the help
of an example.
13. Mention the situations when a copy constructor is invoked?
14. Rewrite the code after removing the errors:
class X
{
public:
int a,b;
void int(void)
{
a=b=0;
}
int sum(void);
int square(void);
};
int sum(void)
{
return a+b;
}
int square(void)
{
return sum() *sum();
}

15. class ABC


{
int x=10;
float y;
ABC(){ y=5; }
~AB?C() {}
};
void main()
{
ABC a1,a2;
}
16. Answer the questions(i) and (ii) after going through the following class :
class Exam
{
int year;
public :
Exam(int y) { year=y; }
Exam(Exam &t);
}
(i) Create an object, such that it invokes constructor 1.
(ii) Write complete definition for constructor 2.
17. Define a class named Publisher in C++ with the following descriptions :
private members
Id long
title 40 char
author 40 char
price , stockqty double
stockvalue double
valcal() A function to find
price*stockqty with double as return type
Public members
• a constructor function to initialize price , stockqty
and stockvalue as 0
• Enter() function to input the idnumber , title and
author
• Takestock() function to increment stockqty by
N(where N is passed as argument to this function)
and call the function valcal() to update the
stockvalue().
• sale() function to decrease the stockqty by N (where
N is sale quantity passed to this function as
argument) and also call the function valcal() to
update the stockvalue
• outdata() function to display all the data members on
the screen.
18. Considering the following specifications :
Structure name data type size
Name first char array 40
mid char array 40
last char array 60
Phone area char array 4
exch char array 4
numb char array 6
Class name Data Type
P_rec name Name
phone Phone
Member functions:
• Define constructor (outside the class P_rec) that
accepts the values of data members from the user.
• Define the display_rec (outside the class P_rec) that
shows the current values .
Declare structures in C++ for Name and Phone . Declare the class
P_rec.
18. Declare a class bank to represent bank account of 10 customers with the
following data members: name of depositor, account number, type of account(s for
savings and c for current account), balance amount. The class also contains the
following member functions:
(i) To initialize data members.
(ii) To deposit money
(iii) To withdraw money after checking minimum balance (say
1000)
(iv) To display the data members on screen.
19. Answer the questions (i) and (ii) after going through the following
program :
class game
{ int time;
public:
game() //function 1
{
time = 0;
cout<,”match commences”<<endl;
}
void details() //function 2
{
cout << “inter section basketball match”” <<endl;
}
game (int duration) //function 3
{
time = duration ;
cout << “another match begins now “<,endl;
}
game(game & m) //function 4
{
time = m.duration;
cout << “like previous match” <<endl;
} }

i. Which category of constructor - function 4 belongs to and what is the


purpose of using it?
ii. Write statements that would call the member functions 1 and 3.
20. Answer the questions (i) and (ii) after going through the following class :
class Computer
{
char C_name[20];
char Config[100];
public:
Computer(Computer &obj); // function1
Computer(); //function 2
Computer(char *n,char *C); // function3
};
(i) Complete the definition of the function 1.
(ii) Name the specific feature of the OOPs shown in the
above example.
21. Define a class Teacher with the following class specification:
private members:
name 20 characters
subject 10 characters
Basic, DA, HRA float
salary float
Calculate() function computes the salary and returns it.
salary is sum of Basic, DA and HRA
public members:
ReadData() function accepts the data values and invoke the calculate function.
DisplayData() function prints the data on the screen.
22. Rewrite the given program after correcting all errors.
Class student
{
int age;
char name[25];
student(char *sname, int a)
{
strcpy(name, sname);
age=a;
}
public:
void display()
{
cout<<”age=”<<age;
cout<<”Name=”<<name;
}
};
student stud;
student stud1(“Rohit”, 17);
main()
{
-------
------
}

Вам также может понравиться