Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading

Data Abstraction and Overloading: Two Marks Important Questions and Answers

Data Structures using C ++ Program

Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading: Anna University Part A Two Marks Important Questions and Answers

Data Structures using C PlusPlus:

Chapter 1: Data Abstraction and Overloading

 

Two Marks Important Questions and Answers


38. Write a C++ code to display "pen object instantiated" and "pen object destroyed" when class for pen constructor and destructor is called.

Answer:

#include <iostream.h>

class Pen

{

private:

   double a;

public:

   void setData( double x);

   double getData( void);

   Pen(); // This is the constructor declaration

   ~Pen(); // This is the destructor: declaration

};

Pen::Pen(void)

{

   cout << "Pen Object instantiated" << endl;

}

Pen:: Pen(void)

{

   cout << "Pen Object destroyed" << endl;

}

void Pen::setData(double x)

{

   a = x;

}

double Pen::getData(void)

{

   return a;

}

int main()

{

   Pen obj;

   obj.setData(10.0);

   cout << "a: " << obj.getData()<<endl;

   return 0;

}

Output

Pen Object instantiated

a: 10

Pen Object destroyed

 

39. What is a reference variable ?

Answer:

Reference variable is an another name for already existing variable. These are the kind of variables that are created using &. For example we can create a reference variable x for integer variable i

int i=10;

int &x=i;

40. What is a friend function ?

Answer:

A friend function is a function that is not the member function of a class but it can access the private and protected members of a class. It is denoted using the keyword friend.

41. Define Constructor. List the types of constructor.

Answer:

• Constructor is a special function having same class name. It is basically used to initialize the variables in the class. The constructor does not have any return type.

• Various types of constructors are :

1) Default Constructor or Zero Argument Constructor

2) Parameterized Constructor

3) Overloaded Constructor

4) Copy Constructor

5) Dynamic Constructor

42. What is meant by default constructor?

Answer:

• Default constructor is a kind of constructor to which no argument is passed. It is also called as zero argument constructor.

• It is called default constructor because we need not have to define it explicitly.

• It is called at the time of object creation.

43. State the difference between structure and class.


Structure

1. By default the members of structure are are public.

2. The structure can not be inherited.

3. The structures do not require constructors.

4. A structure contains only data members.

Class

1. By default the members of class private.

2. The class can be inherited.

3. The classes require constructors for initializing the objects.

4. A class contains the data as well as the function members.

44. What is the output of following program, if it is correct? Otherwise indicate mistake:

int I=10;

void main()

{int I=20;

{int I=30;

cout<<I<<<::I;

}}

Answer:

 The output will be 30 10

45. List out the advantage of new operator over malloc[] function.

Answer:

1) new automatically calculates memory size, whereas malloc() requires manual sizeof.

2) new allows initialization of allocated memory, which malloc() cannot do.

3) new is type‒safe because it returns the correct pointer type automatically.

4) new has simpler syntax, making code cleaner and easier to write.

46. What is the need for initialization of object using constructor?

Answer:

1) It automatically initializes data members when the object is created, avoiding uninitialized or garbage values.

2) This initialization help in hiding initialization logic from the user, improving code readability and reducing errors.

47. List the benefits of using static class data. Give an example of a class with static members.

The members of the class must be declared with the keyword static. The static data members have the same properties as that of global variables. When we declare regular variables then multiple copies are created for accessing them by each corresponding object but when the keyword static is associated with any class variable then that means only single copy of that variable must be created and all other objects must share that copy. The static data members can be accessed without any instantiation of class as an  object. The static member functions are not used very frequently in programs. All static variables are initialized to zero before first object is created.

• The syntax of static data declaration and definition is as given below –

class test {

     private:

         static int count; //declaration

          …

};

int test::count=100; //definition without instantiation of class as object

• The static function can not refer to any non static member data in its class. The static function can access only static and class‒specific data. The syntax of static function is as given below ‒

class test()

{

   private:

   …

   public:

   static int fun();//static function definition

   {

 

   }

   };

   void main()

   {

      …

      test::fun(); //function call

      …

   }

 

Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading : Tag: Data Structure, C++, C Programing : Data Structures using C ++ Program - Data Abstraction and Overloading: Two Marks Important Questions and Answers


Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading



Under Subject


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Electron Devices

EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Circuits and Network Analysis

EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Engineering Drawing - Laboratory

ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus - Laboratory

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Devices and Circuits Laboratory

EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation