0% found this document useful (0 votes)
54 views3 pages

Constructor & Destructor (SET - 1)

The document discusses constructors and destructors in C++ classes through examples. It asks questions about three code examples that demonstrate different constructor definitions and overloading. Specifically: 1) The first example defines a Seminar class with default and parameterized constructors that output messages, and a destructor. Questions ask to call the constructors and identify the destructor. 2) The second example defines a Test class with multiple constructor definitions that initialize string and integer members. Questions ask to call the constructors and identify the demonstrated OOP feature. 3) The third example defines a Sample class with four constructor declarations. Questions ask to define the constructors to initialize private members in different ways.

Uploaded by

Deepak Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views3 pages

Constructor & Destructor (SET - 1)

The document discusses constructors and destructors in C++ classes through examples. It asks questions about three code examples that demonstrate different constructor definitions and overloading. Specifically: 1) The first example defines a Seminar class with default and parameterized constructors that output messages, and a destructor. Questions ask to call the constructors and identify the destructor. 2) The second example defines a Test class with multiple constructor definitions that initialize string and integer members. Questions ask to call the constructors and identify the demonstrated OOP feature. 3) The third example defines a Sample class with four constructor declarations. Questions ask to define the constructors to initialize private members in different ways.

Uploaded by

Deepak Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Constructor & Destructor

[SET 1]
1

Answer the questions (i) and (iii) after going through the
following class:
class Seminar
{
int time;
public:
Seminar()
//Function 1
{
time = 30;
cout << "Seminar starts now" << endl;
}
void lecture()
//Function 2
{
cout << "Lectures in the seminar on" <<
endl;
}
Seminar(int duration)
//Function 3
{
time = duration;
cout << "Seminar starts now" << endl;
}

};

~Seminar()
//Function 4
{
cout << "Thanks" << endl;
}

i. Write statements in C++ that would execute Function 1 and


Function 3 of class Seminar.
ii. In Object Oriented Programming, what is Function 4
referred as and when does it get invoked/called?
iii. In Object Oriented Programming, which concept is
illustrated by Function 1 and Function 3 together?

Answer the questions (i) and (ii) after going through the
following class:
class Test
{

char paper[20];
int marks;
public:
Test ()
// Function 1
{
strcpy (paper, "Computer");
marks = 0;
}
Test (char p[])
// Function 2
{
strcpy(paper, p);
marks = 0;
}
Test (int m)
// Function 3
{
strcpy(paper,"Computer");
marks = m;
}

};

Test (char p[], int m)


{
strcpy (paper, p);
marks = m;
}

// Function 4

i. Write statements in C++ that would execute Function 1,


Function 2, Function 3 and Function 4 of class Test.
ii. Which feature of Object Oriented Programming is
demonstrated using Function 1, Function 2, Function 3 and
Function 4 together in the above class Test?

Consider the definition of the following class:


class Sample
{
private:
int x;
double y;
public :
Sample(); //Constructor 1
Sample(int); //Constructor 2
Sample(int, int); //Constructor 3
Sample(int, double); //Constructor 4
};

i. Write the definition of the constructor 1 so that the private


member variables are initialized to 0.
ii. Write the definition of the constructor 2 so that the private
member variable x is initialized according to the value of the
parameter, and the private member variable y is initialized to
0.
iii. Write the definition of the constructors 3 and 4 so that the
private
member variables are initialized according to the values of the
parameters.

You might also like