Object Oriented Programming - C++ : Chapter 2: Classes and Objects : Anna University Part A Two Marks Important Questions and Answers
Object
Oriented Programming- C++
Chapter
2: Classes and Objects
Two Marks
Questions with Answers
1. What
is dynamic constructor? Give example.
Answer:
Dynamic constructor is a kind of constructor which can allocate the memory for
the objects using the operator new
For example ‒
TEST::TEST() //dynamic constructor
{
len=0; //initially length is 0
str=new char[len+1];
strcpy(str,""); //initialization by empty string
}
2. Write
copy constructor for class date(assume mm,dd,yy as its members).
Answer:
#include<iostream.h>
class CopyCons
{
int dd,mm,yy;
public:
CopyCons();
CopyCons(int d,int
m,int y){dd=d;mm=m;yy=y;
}
CopyCons(CopyCons &x)
{
dd=x.dd;
mm=x.mm;
yy=x.yy;
}
void display()
{
cout<<"\nDate:
"<<dd;
cout<<"\nMonth:
"<<mm;
cout<<"\nYear:
"<<yy;
}
};
int main()
{
CopyCons
X(10,10,1999);
CopyCons Y(X);
CopyCons Z=X;
cout<<"\n
Date using object X: \n";
X.display();
cout<<"\n
Date using object Y: \n";
Y.display();
cout<<"\n
Date using object Z: \n";
Z.display();
return 0;
}
3. When
will the destructor be called? Is it implicit or explicit ?
Answer: The destructor
will be called only when on wants to deallocate the memory. This can be
explicitly created using ~ followed by class name function or when the program
reached to end, when is encountered then automatically the implicit destructor
is called in order to deallocate the memory. It is a good practice to write the
explicit destructor to dealllocate the allocated memory for the object.
4. What
are copy constructors ?
Answer:
Copy constructor is a special type of constructor in which new object is
created as a copy of existing object.
5. What is
instantiation ?
Answer: Instantiation
of object is needed to use the object of particular class. The class is a kind
of template and by instantiation of the object, we can make use of the data
members and member function of class.
6. How is
class declared in C++?
Answer:
The class is declared using the keyword class. Following example shows how to
declare class in C++.
Syntax
class classname
{
access specifier:
data members;
access specifier
operations;
};
Example
class Student
{
private:
int rollno;
float marks;
public:
void
input_marks();
void
display_marks();
};
7. What
is scope resolution operator and how it can be used for global variable ?
Answer: The scope
resolution operator is denoted by ::. It is used to define the scope of the
variable or a function. If we write some function inside the class, then that
function can be accessed from outside the class using the scope resolution of
operator. Thus the scope of the scope resolution operator is global.
8. Highlight
the advantages of static data members and static functions in C++.
Answer:
When a static data member or a static function is created then that means only
one copy of that variable or function is created and all other objects must
share that single copy. Thus some amount of memory gets saved. Secondly static
members can be accessed without any instantiation of class as an object.
9. What
is destructor?
Answer:
The destructor is a special function which is called when the object is
destroyed. The destructor function name is preceded by the tilde sign(~).
10. Give
any two characteristics of constructors
Answer:
1.
The name of the constructor and the class name must be same.
2.
The constructor must be declared in public mode.
3.
The constructors can not be inherited.
11. Can
the parameter of copy constructor be passed by value ? Justify your answer.
Answer:
No the parameter of copy constructor can not be passed by value. Because the
copy constructor is a special type of constructor in which new object is created
as a copy of existing object. Hence copy constructor always takes a reference
(address) to an object of a same class as an argument.
12. How do the constructor gets invoked?
Answer:
The constructor need not be invoked explicitly, instead, it gets invoked
automatically when the object of particular class is created.
13. Define
attribute.
Answer:Attributes
are the data members of the class. These are meant for representing the
characteristics of the class.
14. What
effects do the visibility labels private, protected and public have on the
members of the class ?
Answer:
If the base class has the members possessing the visibility label as private
then those members are not accessible to derived class. If the members of the
class are labelled as protected then that means these members are public to
derived class but are private to rest of the program. If the members of the
class are labelled as public then that indicates the members can be accessible
by any class in the entire program.
15. What
is the default access specifier of a class?
Answer:
The default access specifier is private.
16. What is the difference between private,
protected and public modes ?
Answer:
If the base class have private members then those members are not accessible to
derived class. Protected members are public to derived classes but private to
reset of the program. And the public members are accessible to all.
17. What
are the points to remember about friend function ?
Answer:
The friend function is always preceded by the keyword friend. It is not a
member of a class but can access the private and protected members of the
class.
18. What
are merits of using Classes ?
Answer:
1.
The data and member function can be encapsulated within one entity.
2.
The data hiding property can be achieved.
3.
The data can be reused by another class by the OOP feature called inheritance
4.
Multiple instances of the same class can be created to handle set of data
separately.
19. What is the use of destructor ?
Answer:
When the programmer wants to deallocate the memory being allocated then
destructor is called. The destructor uses ~ followed by class name function.
Object Oriented Programming: Chapter 2: Classes and Objects : Tag: Oops, Computer Programming : Object Oriented Programming - C++ - Classes and Objects: Two Marks Important Questions and Answers
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs) - Laboratory
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation