Samacheer Kalvi Class 11 Computer Science Solutions Chapter 14 Classes and Objects

Get the most accurate TN Board Solutions for Class 11 Computer Science Chapter 14 Classes and Objects here. Updated for the 2026-27 academic session, these solutions are based on the latest TN Board textbooks for Class 11 Computer Science. Our expert-created answers for Class 11 Computer Science are available for free download in PDF format.

Detailed Chapter 14 Classes and Objects TN Board Solutions for Class 11 Computer Science

For Class 11 students, solving TN Board textbook questions is the most effective way to build a strong conceptual foundation. Our Class 11 Computer Science solutions follow a detailed, step-by-step approach to ensure you understand the logic behind every answer. Practicing these Chapter 14 Classes and Objects solutions will improve your exam performance.

Class 11 Computer Science Chapter 14 Classes and Objects TN Board Solutions PDF

Book Evaluation

Part I

Choose The Correct Answer

 

Question 1. The variables declared inside the class are known as data members and the functions are known as
(a) data functions
(b) inline functions
(c) member functions
(d) attributes
Answer: (c) member functions
In simple words: Inside a class, variables are called data members, and functions are called member functions. These help organize data and actions together.

🎯 Exam Tip: Remember that data members hold information, while member functions perform actions or operations on that data.

 

Question 2. Which of the following statements about member functions are True or False?
(i) A member function can call another member function directly with using the dot operator.
(ii) Member function can access the private data of the class.
(a) i-True, ii-True
(b) i-False, ii-True
(c) i-True, ii-False
(d) i-False, ii-False
Answer: (b) i-False, ii-True
In simple words: Member functions can directly access a class's private data, which is their main purpose. However, they call other member functions directly without needing the dot operator within the same class.

🎯 Exam Tip: Always recall that a key benefit of member functions is their ability to interact with private data, ensuring data safety and encapsulation.

 

Question 3. A member function can call another member function directly, without using the dot operator called as
(a) sub function
(b) sub member
(c) nesting of member function
(d) sibling of member function
Answer: (c) nesting of member function
In simple words: When one member function calls another member function from the same class without using the dot operator, it's called nesting of member functions. This helps in organizing code within a class.

🎯 Exam Tip: Nesting functions improves code readability and organization within a class, making complex operations easier to manage.

 

Question 4. The member function defined within the class behave like
(a) inline functions
(b) Non inline function
(c) Outline function
(d) Data function
Answer: (a) inline functions
In simple words: Member functions written entirely inside a class definition are treated by the compiler as inline functions. This often makes small functions run faster.

🎯 Exam Tip: Inline functions can speed up program execution by replacing function calls with the function's code directly, though it can increase code size for larger functions.

 

Question 5. Which of the following access specifier protects data from inadvertent modifications?
(a) Private
(b) Protected
(c) Public
(d) Global
Answer: (a) Private
In simple words: The 'private' access specifier keeps data hidden and safe from accidental changes by parts of the program outside the class. Only functions within the class can touch private data.

🎯 Exam Tip: Data hiding, achieved using 'private' members, is a core principle of object-oriented programming for maintaining data integrity.

 

Question 6. How many objects are created for the above program?
class x
{
int y;
public:
x(int z)
{
y=z;
}
} x1[4];
int main( )
{
x x2(10);
return 0;
}
(a) 10
(b) 14
(c) 5
(d) 2
Answer: (c) 5
In simple words: In this code, `x1[4]` creates an array of 4 objects, and `x x2(10)` creates one more object. So, in total, 4 plus 1 makes 5 objects.

🎯 Exam Tip: Remember that `x1[4]` declares an array of four objects, each being an instance of class `x`.

 

Question 7. State whether the following statements about the constructor are True or False.
(i) constructors should be declared in the private section.
(ii) constructors are invoked automatically when the objects are created.
(a) True, True
(b) True, False
(c) False, True
(d) False, False
Answer: (c) False, True
In simple words: Constructors are special functions that run on their own when a new object is made. They are usually put in the public part of a class so they can be easily used to create objects.

🎯 Exam Tip: Constructors are typically public to allow external code to create and initialize objects correctly. Making them private would restrict object creation.

 

Question 8. Which of the following constructor is executed for the following prototype ?
add display (add &); // add is a class name
(a) Default constructor
(b) Parameterized constructor
(c) Copy constructor
(d) Non Parameterized constructor
Answer: (c) Copy constructor
In simple words: The prototype `add display (add &)` shows a function that takes an object of type `add` by reference. This pattern is exactly what a copy constructor uses to make a new object from an existing one.

🎯 Exam Tip: A copy constructor always takes an object of its own class as a reference parameter, like `ClassName(const ClassName& otherObject)`. This is a key identifier.

 

Question 9. What happens when a class with parameterized constructors and having no default constructor is used in a program and we create an object that needs a zero- argument constructor?
(a) Compile-time error
(b) Domain error
(c) Runtime error
(d) Runtime exception
Answer: (a) Compile-time error
In simple words: If you create a class with a constructor that requires parameters but don't provide a default (no-parameter) constructor, the program will show an error when you try to create an object without any arguments. The compiler needs to know how to build the object.

🎯 Exam Tip: If you define any parameterized constructor, the compiler will *not* generate a default constructor automatically. You must define one explicitly if you need to create objects without arguments.

 

Question 10. Which of the following create a temporary instance?
(a) Implicit call to the constructor
(b) Explicit call to the constructor
(c) Implicit call to the destructor
(d) Explicit call to the destructor
Answer: (b) Explicit call to the constructor
In simple words: An explicit call to a constructor, like `ClassName(arguments)`, directly creates an object. This object is often temporary if not assigned to a variable, used for a brief calculation, and then disappears.

🎯 Exam Tip: Explicit calls like `return Type(value);` or `function(Type(value));` commonly lead to temporary objects being created.

Part – II

Very Short Answers

 

Question 1. What are called members?
Answer: Members are the parts that make up a class. They are divided into two main types: data members and member functions. Data members are like variables that store information about the class, showing its features. Member functions are like actions or tasks that the class can perform. Together, they define what an object of that class can be and do.
In simple words: Members are the variables (data members) and functions (member functions) inside a class. They describe what a class is and what it can do.

🎯 Exam Tip: Clearly distinguishing between data members (for state) and member functions (for behavior) is crucial for understanding object-oriented programming.

 

Question 2. Differentiate structure and class though both are user-defined data types.
Answer: Both structures and classes allow you to create your own data types. The main difference between a structure and a class is how their members are handled by default. In a structure, all members (variables and functions) are public by default, meaning they can be accessed from anywhere. In contrast, in a class, all members are private by default, which means they can only be accessed from within that class itself. This privacy helps protect data.
In simple words: Structures have public members by default, while classes have private members by default. Both let you make new data types.

🎯 Exam Tip: The default access specifier is the primary conceptual difference: `struct` defaults to public, `class` defaults to private. This choice influences data protection.

 

Question 3. What is the difference between the class and object in terms of oop?
Answer:
Object:

  • An object is a specific example or "instance" of a class.
  • It is like a real-world item, such as a pen, a laptop, a mobile phone, or a chair.
  • An object actually takes up space in the computer's memory when it is created.

Class:
  • A class is like a blueprint or a detailed plan from which objects are created.
  • It is a way to group similar objects together, defining their common features and actions.
  • A class itself does not take up memory; it only describes what an object will look like when it's built.

In simple words: A class is a blueprint, like a house plan. An object is the actual house built from that plan. The object exists and uses memory, but the class is just a design.

🎯 Exam Tip: Think of a class as a cookie cutter (the definition), and an object as the actual cookie (an instance made from the definition).

 

Question 4. Why it is considered a good practice to define a constructor though a compiler can automatically generate a constructor?
Answer: It is good practice to define your own constructor because a user-defined constructor offers the best way to properly set up arrays of objects and regular objects. While the compiler can create a default constructor automatically, this automatic constructor might not initialize all members to sensible starting values, especially if the class contains complex data types. By defining your own constructor, you ensure that objects are always created in a valid and predictable state, preventing potential errors or unexpected behavior later in the program.
In simple words: It's better to make your own constructor because it lets you properly set up all parts of an object when it's made. The computer's automatic constructor might not always set everything correctly.

🎯 Exam Tip: Always initialize all data members in your constructor to ensure objects are created in a valid and predictable state, avoiding garbage values and unexpected program behavior.

 

Question 5. Write down the importance of the destructor.
Answer: The destructor is very important because its main job is to release any resources that an object might have taken up while it was active. For example, if an object allocated memory during its lifetime, the destructor function makes sure that memory is given back to the system when the object is no longer needed. This prevents memory leaks, where memory is taken but never returned, which can slow down or crash a program over time.
In simple words: The destructor's job is to clean up. It frees up any computer resources, like memory, that an object used while it was running, making sure nothing is wasted.

🎯 Exam Tip: Destructors are essential for resource management, particularly for dynamically allocated memory or file handles, to prevent resource leaks.

Part – III

Short Answers

 

Question 1. Rewrite the following program after removing the syntax errors if any and underline the errors:
#include<iostream>
#include<stdio.h>
class mystud
{ intstudid =1001;
char name[20];
public
mystud( )
{ }
void register ( ) {cin>>stdid;gets(name);
}
void display ( )
{ cout<}
int main( )
{ mystud MS;
register.MS( );
MS.display( );
}
Answer:
MODIFIED PROGRAM:
#include<iostream>
#include<stdio.h>
class mystud
{
int studid; // Error 1: Initialization of non-static member at declaration is not allowed in C++98, moved to constructor.
char name[20];
public:
mystud( )
{
studid=1001;
}
void register_student ( ) // Error 2: `register` is a keyword, changed to `register_student`.
{
cin>>studid;
gets(name);
}
void display ( )
{
cout<}
}; // Error 3: Missing semicolon after class definition.
int main( )
{
mystud MS;
MS.register_student( ); // Error 4: Calling member function with object `MS`. `register` was also a keyword.
MS.display( );
return 0;
}
In simple words: The original program had a few mistakes. First, a variable was given a value right where it was declared inside the class, which is not allowed unless it's a special type of variable. This was fixed by moving the value assignment into the constructor. Second, the word "register" was used for a function name, but "register" is a special word in C++, so it was changed to "register_student". Third, a semicolon was missing after the class definition, which is needed in C++. Lastly, the function `register_student` needed to be called using the object `MS`, like `MS.register_student()`, not `register.MS()`.

🎯 Exam Tip: Pay close attention to C++ keywords (like `register`), proper member initialization, correct syntax (like semicolons after class definitions), and how to call member functions using objects.

 

Question 2. Write with an example of how will you dynamically initialize objects?
Answer: Dynamic initialization of objects means giving them their first values while the program is actually running, not when the code is first written. This is useful when the initial values are not known until runtime, perhaps because they depend on user input or calculations. The following program illustrates this concept:
Program to illustrate dynamic initialization
#include <iostream>
using namespace std;
class X
{
int n;
float avg;
public:
X(int p,float q)
{
n=p;
avg=q;
}
void disp( )
{
cout<<"\nRoll numbe:-" << n;
cout<<"\nAverage :-"<< avg;
}
};
int main( )
{
int a; float b;
cout<<"\nEnter the Roll Number";
cin>>a;
cout<<"\nEnter the Average";
cin>>b;
X x(a,b); // dynamic initialization
x.disp( );
return 0;
}
Output
Enter the Roll Number 1201
Enter the Average 98.6
Roll number:- 1201
Average :- 98.6
In simple words: Dynamic initialization means you give values to an object when the program is running, not when you write the code. For example, a program might ask the user for a roll number and average, and then use those numbers to create an object, giving it values on the fly.

🎯 Exam Tip: Dynamic initialization is most often achieved through parameterized constructors where values are passed at the time of object creation, allowing for flexible object setup based on runtime data.

 

Question 3. What are advantages of declaring constructors and destructor under public access ability?
Answer: Declaring constructors and destructors in the public section of a class offers several advantages:
1. We can initialize the object while declaring it. This means objects can be set up correctly as soon as they are created.
2. We can explicitly call the constructor when needed, giving more control over object creation.
3. We can overload constructors, allowing for multiple ways to initialize objects automatically with different sets of input.
4. We can destroy the objects automatically at the end of their scope, which helps free up unused memory and prevent resource leaks. Sometimes, certain C++ compilers might not even allow constructors and destructors to be declared in the private section. Therefore, it is generally better to declare them in the public section for wider compatibility and accessibility.
In simple words: Putting constructors and destructors in the public part of a class lets us easily create, set up, and clean up objects. It allows us to give objects starting values, use different ways to make them, and ensure memory is freed automatically when objects are no longer needed.

🎯 Exam Tip: Declaring constructors and destructors as public is standard practice because it enables proper object lifecycle management from outside the class, which is fundamental to OOP.

 

Question 4. Given the following C++ code, answer the questions (i) & (ii).
class TestMeOut
{
public:
~TestMeOut( ) //Function 1
{
cout<<"Leaving the examination hall"<}
TestMeOut( ) //Function 2
{
cout<<"Appearing for examination'"<}
void MyWork( ) //Function 3
{
cout<<"Attempting Questions//"<}
};
i) In Object-Oriented Programming, what is Function 1 referred to as and when does it get invoked/called?
ii) In Object-Oriented Programming, what is Function 2 referred to as, and when does it get invoked/called?
Answer:
i) Function 1, `~TestMeOut()`, is called a destructor. It gets invoked automatically when an object of the `TestMeOut` class goes out of scope, which typically happens at the end of the program or when the block of code where the object was created finishes. Destructors perform cleanup tasks, like freeing memory.
ii) Function 2, `TestMeOut()`, is called a constructor. It gets invoked automatically when an object of the `TestMeOut` class is created. Constructors initialize objects, setting their initial state. In this example, it prints a message when an object begins its life.
In simple words: Function 1, with the tilde (`~`), is the "destructor" and runs when an object is finished and leaves the program. Function 2, with no tilde, is the "constructor" and runs right when a new object is made to set it up.

🎯 Exam Tip: Identify constructors and destructors by their names matching the class name; the destructor is always prefixed with a tilde (`~`). Remember their automatic invocation at object creation and destruction, respectively.

 

Question 5. Write the output of the following C++ program code:
#include<iostream>
using namespace std;
class Calci
{
char Grade;
int Bonus;
public:
Calci( )
{
Grade='E';
Bonus=0;
}//ascii value of A=65
void Down(int G)
{
Grade-=G;
}
void Up(int G)
{
Grade+=G;
Bonus++;
}
void Show( )
{
cout<<Grade<<"#"<<Bonus<<endl;
}
};
int main( )
{
Calci c;
c.Down(3);
c.Show( );
c.Up(7);
c.Show( );
c.Down(2);
c.Show( );
return 0;
}
Answer:
The program will produce the following output:
B#0
I#1
G#1

Explanation:
1. `Calci c;`: An object `c` is created. Constructor initializes `Grade = 'E'` (ASCII 69) and `Bonus = 0`.
2. `c.Down(3);`: `Grade` becomes `Grade - 3` (69 - 3 = 66), which is 'B' (ASCII 66). `Bonus` remains 0.
3. `c.Show();`: Prints `B#0`.
4. `c.Up(7);`: `Grade` becomes `Grade + 7` (66 + 7 = 73), which is 'I' (ASCII 73). `Bonus` becomes `0 + 1 = 1`.
5. `c.Show();`: Prints `I#1`.
6. `c.Down(2);`: `Grade` becomes `Grade - 2` (73 - 2 = 71), which is 'G' (ASCII 71). `Bonus` remains 1.
7. `c.Show();`: Prints `G#1`.
In simple words: The program starts by setting Grade to 'E' and Bonus to 0. Then, it changes the Grade letter and Bonus number step-by-step using the `Down` and `Up` functions, showing the result after each pair of steps. The Grade changes based on its ASCII value, and Bonus only goes up when `Up` is called.

🎯 Exam Tip: When tracing character arithmetic, remember to use ASCII values (`A` is 65, `B` is 66, etc.) to perform calculations correctly and then convert back to the character.

Part – IV

Explain In Detail

 

Question 1. Explain nested class with example.
Answer: A nested class happens when one class is made a member of another class. This special relationship is often called 'containership'. In this setup, the class that is defined inside is known as the "Nested class" (or inner class), and the class that contains it is called the "Enclosing class" (or outer class). A nested class can be placed in either the private or public part of the enclosing class, depending on how you want to control its access. This technique helps organize complex code by grouping related classes together. For example, a car class might contain a nested engine class.

Classes can be nested in two main ways:
1. By defining a class directly inside another class.
2. By declaring an object of one class as a member inside another class.

Here’s a C++ program to show a nested class:
#include<iostream>
using namespace std;
class enclose
{
private:
int x;
class nest
{
private :
int y;
public:
int z;
void prn( )
{
y=3;z=2;
cout<<"\n The product of"
<< y <<"*"<< z<<"= "<< y*z <<"\n";
}
}; //inner class definition over
nest m1;
public:
nest n2;
void square( )
{
n2.prn( ); //inner class member function is called by its object
x=2;
n2.z=4;
cout<<"\n The product of" <<n2.z<<"*"<<n2.z<<"="<<n2.z*n2.z<<"\n";
cout<<"\n The product of" <<x<<"*"<<x<<"= "<<x*x;
}
}; //outer class definition over
int main( )
{
enclose e;
e.square( ); //outer class member function is called
}
Output:
The product of 3*2=6
The product of 4*4=16
The product of 2*2=4

In the program above, the `nest` class is defined inside the `enclose` class. The `nest` class is then accessed through an object of the `enclose` class. This example demonstrates how the inner class's members can be used by the outer class's functions.
In simple words: A nested class is a class inside another class. The inside class is "nested," and the outside one is "enclosing." This helps organize related code. You can either put the whole class inside or just make an object of one class part of another. The example shows how an outer class can use functions and data from its inner class.

🎯 Exam Tip: Remember that nested classes help in organizing code and hiding implementation details, much like private members. The outer class can access private members of the inner class, but not vice-versa.

 

Question 2. Mention the differences between constructor and destructor.
Answer: Constructors and destructors are special member functions in a class that perform specific actions. Here are the key differences between them:

CONSTRUCTORDESTRUCTOR
The name of the constructor must be the same as that of the class.The destructor has the same name as that of the class, prefixed by the tilde character.
A constructor can have a parameter list.The destructor cannot have arguments.
The constructor function can be overloaded.Destructors cannot be overloaded, meaning there can be only one destructor in a class.
A constructor cannot be inherited, but a derived class can call the base class constructor.A destructor cannot be inherited.
The constructor is executed automatically when the object is created.The destructor is executed automatically when the control reaches the end of the class.
Allocates memory space for the object.Destroys the object.

In simple words: A constructor builds and sets up an object when it is made, often having the same name as the class. A destructor cleans up and removes the object when it is no longer needed. It also has the class name, but with a special symbol (~) in front.

🎯 Exam Tip: Remember that constructors help initialize objects, while destructors clean up resources. Focus on their roles in object lifecycle and their unique naming conventions for full marks.

 

Question 3. Define a class RESORT with the following description in C++ :
Answer: Here is a C++ class definition for `RESORT` based on the given description, including private and public members, along with a program example. The class manages resort room bookings, calculating charges and applying a bonus if the total amount exceeds a certain value.
Private members:
Rno // Data member to storeroom number
Name //Data member to store user name
Charges //Data member to store per day charge
Days //Data member to store the number of days
Compute ( ) // A function to calculate total amount as Days * Charges and if the //total amount exceeds 11000 then total amount is 1.02 * Days *Charges
Public member:
getinfo( ) // Function to Read the information like name , room no, charges and days
dispinfo ( ) // Function to display all entered details and total amount calculated
//using COMPUTE function
PROGRAM

#include<iostream>
using namespace std;
class RESORT
{
private:
    int Rno,Days,Charges;
    char Rname[20];
    int compute()
    {
        if (Days * Charges > 11000)
            return (Days * Charges * 1.02);
        else
            return(Days * Charges);
    }
public:
    void getinfo()
    {
        cout<<"\nEnter customer name : ";
        cin>>Rname;
        cout<<"\nEnter charges per day : ";
        cin>>Charges;
        cout<<"\nEnter Number of days : ";
        cin>>Days;
        cout<<"\nEnter Room Number : ";
        cin>>Rno;
    }
    void dispinfo()
    {
        cout<<"\nRoom Number : "<

In simple words: This C++ code defines a `RESORT` class to handle customer booking information. It takes details like customer name, room number, daily charges, and number of days. It then calculates the total amount, adding a small bonus if the total goes over Rs. 11,000, and displays all the information.

🎯 Exam Tip: When defining a class, ensure private members are correctly encapsulated and public methods provide controlled access. Pay close attention to conditional logic in calculations to apply rules like bonus charges accurately.

 

Question 4. Write the output of the following:
Answer: Here is the C++ program code and its expected output. The program demonstrates the use of nested classes and constructors to manage student and subject data.

#include<iostream>
#include<stdio.h>
using namespace std;
class sub
{
public :
    int day, subno;
    sub(int d=150,int sn=12) // prototype
    {
        cout<<endl<<"Constructing the object "<<endl;
        day=d;
        subno=sn;
    }
    void printsub( )
    {
        cout<<" subject number: "<<subno;
        cout<<" Days : "<<day;
    }
};
class stud
{
public:
    int rno;
    float marks;
    stud( )
    {
        cout<<"Constructing the object of students "<<endl;
        rno=0;
        marks=0.0;
    }
    void getval( )
    {
        cout<<"Enter the roll number and the marks secured";
        cin>>rno>>marks;
    }
    void printdet( )
    {
        cout<<"Roll no : "<<rno<<"Marks : "<<marks<<endl;
    }
};
class admission
{
public :
    sub obj;
    stud objone;
    float fees;
    admission ( )
    {
        cout<<"Constructing the object of admission "<<endl;
        fees=0.0;
    }
    void printdet( )
    {
        objone.printdet( );
        obj.printsub();
        cout<<"fees : "<<fees<<endl;
    }
};
int main( )
{
    system ("cls");
    admission adm;
    cout<<endl<< "Back in main ()";
    return 0;
}

Output:

Constructing the object
Constructing the object of students
Constructing the object of admission
Back in main ()
Process exited after 0.1532 seconds with return value 0
Press any key to continue . . .

In simple words: This program creates an admission object, which in turn creates a subject object and a student object. When each of these objects is made, a message saying "Constructing the object" for each is printed to the screen. The program then prints "Back in main ()" before ending.

🎯 Exam Tip: When tracing constructor calls in nested classes, remember that outer class constructors are called after inner class constructors. Also, `system("cls")` clears the console, so initial output might not be visible depending on the environment.

 

Question 5. Write the output of the following.
Answer: This C++ program demonstrates the order of constructor and destructor calls for nested objects within a class hierarchy.

#include<iostream>
#include<stdio.h>
using namespace std;
class P
{
public:
    P( )
    {
        cout<<"\nConstructor of class P ";
    }
    ~P( )
    {
        cout<<"\nDestructor of class P ";
    }
};
class Q
{
public:
    Q( )
    {
        cout<<"\nConstructor of class Q ";
    }
    ~Q( )
    {
        cout<<"\nDestructor of class Q ";
    }
};
class R
{
public:
    P obj1, obj2;
    Q obj3;
    R( )
    {
        cout<<"\nConstructor of class R ";
    }
    ~R( )
    {
        cout<<"\nDestructor of class R ";
    }
};
int main ( )
{
    R oR;
    Q oq;
    return 0;
}

Output:

Constructor of class P
Constructor of class P
Constructor of class Q
Constructor of class R
Constructor of class Q
Destructor of class Q
Destructor of class R
Destructor of class Q
Destructor of class P
Destructor of class P
Process exited after 0.22 seconds with return value 0
Press any key to continue . . .

In simple words: When the program runs, it first creates objects inside class R, calling their constructors (P, P, Q) and then the R constructor. After that, it creates a Q object and calls its constructor. When the program finishes, the destructors are called in reverse order for all the objects, cleaning them up.

🎯 Exam Tip: The order of constructor calls for nested objects is usually from innermost to outermost, and destructor calls happen in the reverse order. Pay attention to how nested objects are declared within a class to predict this flow accurately.

11th Computer Science Guide Classes and Objects Additional Questions and Answers

Choose The Correct Answer (1 Mark)

 

Question 1. The most important feature of C++ is ………………..
(a) object
(b) class
(c) public
(d) All the options
Answer: (d) All the options
In simple words: The main special thing about C++ is that it uses objects, classes, and public access, which are all part of how it works together.

🎯 Exam Tip: While classes are central to C++, the strength of OOP comes from the combination of features like objects, classes, and access specifiers. For MCQs, consider the broadest correct answer.

 

Question 2. How many features are commonly present in OOP languages?
(a) 3
(b) 2
(c) 4
(d) 5
Answer: (c) 4
In simple words: Most object-oriented programming languages have four main features: abstraction, encapsulation, inheritance, and polymorphism.

🎯 Exam Tip: Remember the four pillars of OOP: Abstraction, Encapsulation, Inheritance, and Polymorphism. Knowing these terms helps answer questions about OOP principles.

 

Question 3. Calling a member function of an object is also known as ……………….. to object.
(a) call function
(b) call by value
(c) call by reference
(d) sending message
Answer: (d) sending message
In simple words: When you tell an object to do something by using one of its functions, it is like sending a message to that object.

🎯 Exam Tip: In OOP, interacting with an object's functions is often described as "sending a message" to the object, emphasizing the communication aspect between objects.

 

Question 4. ……………… is a way to bind the data and its associated functions together,
(a) Class
(b) Array
(c) Structure
(d) All the options
Answer: (a) Class
In simple words: A class is like a container that keeps related information (data) and the actions that can be done with that information (functions) all in one place.

🎯 Exam Tip: This question refers to encapsulation, a core OOP concept. Remember that a class bundles data and methods together into a single unit.

 

Question 5. When one class become a member of another class, the relationship is called ………………..
(a) containership
(b) partnership
(c) friendship
(d) all the options
Answer: (a) containership
In simple words: When one class is part of another class, it is called containership, like a box containing other items.

🎯 Exam Tip: Containership (also known as aggregation or composition) is when one class holds objects of another class. Do not confuse it with inheritance, where a class gets features from another.

 

Question 6. The body of the class is defined inside the ………………. brackets.
(a) Angle < >
(b) Square [ ]
(c) Curly { }
(d) None of these
Answer: (c) Curly { }
In simple words: In C++, you always use curly brackets, like { and }, to show where a class starts and ends.

🎯 Exam Tip: Always remember the basic C++ syntax: curly braces `{}` define code blocks for classes, functions, and control structures.

 

Question 7. ……………….. can be defined either in private or in the public section of a class.
(a) Object
(b) Data type
(c) Memory
(d) constructor
Answer: (d) constructor
In simple words: A constructor is a special function that can be placed in either the private or public part of a class.

🎯 Exam Tip: While constructors are typically public to allow object creation, they can be private for specific design patterns, such as singleton pattern, restricting direct instantiation.

 

Question 8. The members of the structure are by default ………………..
(a) Private
(b) Public
(c) Protected
(d) None of these
Answer: (b) Public
In simple words: In a structure, all its parts (members) are open for anyone to use by default, unless you say otherwise.

🎯 Exam Tip: Remember the key difference between `struct` and `class` in C++: members of a `struct` are public by default, while members of a `class` are private by default.

 

Question 9. There are ……………….. ways to create an object using the parameterized constructor.
(a) 3
(b) 2
(c) 1
(d) 4
Answer: (c) 1
In simple words: When you use a parameterized constructor, you're performing one type of action - creating an object with starting values. While there are different ways to write this in code, the core process is focused on this single action.

🎯 Exam Tip: A parameterized constructor allows you to create and initialize an object with specific values at the same time. There are two common syntax styles (implicit and explicit) to do this, but they both achieve the same fundamental goal of creating the object with parameters.

 

Question 10. The class body contains ………………….
(a) Data members
(b) Member functions
(c) Both A and B
(d) None of these
Answer: (c) Both A and B
In simple words: A class is made up of two main parts: data members, which store information, and member functions, which perform actions.

🎯 Exam Tip: A class is a blueprint that encapsulates both data (data members) and the operations that can be performed on that data (member functions) to represent a real-world entity.

 

Question 11. The class body has………………… access specifiers.
(a) Three
(b) Four
(c) Two
(d) Five
Answer: (a) Three
In simple words: A class has three main levels of access: private, protected, and public, which control who can use its parts.

🎯 Exam Tip: The three access specifiers in C++ (private, protected, public) are crucial for controlling visibility and encapsulation within a class. Understand their differences to manage data security.

 

Question 12. The class body has…………….. access specifiers.
(a) Private
(b) Public
(c) Protected
(d) All the options
Answer: (d) All the options
In simple words: The body of a class can have all types of access rules: private, public, and protected, to decide how its parts can be used.

🎯 Exam Tip: This question reinforces the previous one. While there are three *types* of specifiers, a single class can utilize all of them in its definition.

 

Question 13. …………………. is a visibility label.
(a) Private
(b) Public
(c) Protected
(d) All the options
Answer: (d) All the options
In simple words: Private, public, and protected are all labels that show how visible or accessible parts of a class are.

🎯 Exam Tip: Visibility labels (access specifiers) like private, public, and protected determine how class members can be accessed from inside and outside the class. They are fundamental for encapsulation.

 

Question 14. ……………….. allows preventing the functions of a program to access directly the internal representation of a class type.
(a) Data Hiding
(b) Data Capturing
(c) Data Processing
(d) None of these
Answer: (a) Data Hiding
In simple words: Data hiding means keeping the inside details of a class secret, so other parts of the program cannot change them directly, protecting the data.

🎯 Exam Tip: Data hiding, often achieved through private access specifiers, is a key aspect of encapsulation. It ensures that the internal state of an object can only be changed through controlled methods.

 

Question 15. The access restriction to the class members is specified by - section within the class body.
(a) Private
(b) Public
(c) Protected
(d) All of the options
Answer: (d) All of the options
In simple words: The sections like private, public, and protected control how different parts of a class can be used. These sections collectively define the access rules for class members.

🎯 Exam Tip: Understanding access specifiers (private, public, protected) is key to controlling data visibility and promoting encapsulation in object-oriented programming.

 

Question 16. A - member is accessible from where outside the class but within a program.
(a) Private
(b) Public
(c) Protected
(d) All of the options
Answer: (b) Public
In simple words: A public member can be used from anywhere in the program, even outside the class itself. This makes it easy for other parts of the program to interact with the object.

🎯 Exam Tip: Public members act as the interface for your class, allowing other code to interact with your object while keeping internal details hidden.

 

Question 17. We can set and get the value of public data members using - function.
(a) Member
(b) Nonmember
(c) Either A or B
(d) None of these
Answer: (c) Either A or B
In simple words: You can change or get the value of a public data member directly through member functions or even non-member functions in certain cases, making them very flexible. Using member functions is generally safer as it allows for validation.

🎯 Exam Tip: While direct access to public data members is possible, using 'getter' and 'setter' member functions is often preferred for better control and validation of data.

 

Question 18. A - member cannot be accessed from outside the class.
(a) Private
(b) Public
(c) Protected
(d) All of the options
Answer: (a) Private
In simple words: Private members are hidden inside the class and cannot be directly reached by code outside that class. This helps keep the internal workings of the class secure.

🎯 Exam Tip: Private members are fundamental for data hiding and encapsulation, which are core principles of object-oriented programming for maintaining data integrity.

 

Question 19. Only the class member functions can access - members.
(a) Private
(b) Public
(c) Protected
(d) All of the options
Answer: (a) Private
In simple words: Only functions that are part of the same class can see and use its private data. This keeps the data safe from changes by outside code.

🎯 Exam Tip: This restricted access ensures that private data is modified only through controlled methods, preventing accidental corruption and maintaining consistent state.

 

Question 20. - members can be accessed in child classes.
(a) Private
(b) Public
(c) Protected
(d) All of the options
Answer: (c) Protected
In simple words: Protected members are like private members, but with one key difference: child classes (derived classes) can access them. This allows for inheritance while still maintaining some level of data protection.

🎯 Exam Tip: Protected members are crucial for building class hierarchies, as they allow derived classes to interact with parent class data while keeping it inaccessible to external code.

 

Question 21. If all members of the class are defined as - then the class become frozen.
(a) Private
(b) Public
(c) Protected
(d) All of the options
Answer: (a) Private
In simple words: If every part of a class is set to "private," it becomes unusable from the outside, like being "frozen." No other part of the program can interact with it, making it pretty useless.

🎯 Exam Tip: A class with all private members is not practically useful unless it is designed purely for internal processing without any external interaction points.

 

Question 22. If all members of the class are defined as - then the object of the class can not access anything from the class,
(a) Private
(b) Public
(c) Protected
(d) All of the options
Answer: (a) Private
In simple words: When everything in a class is private, an object made from that class cannot reach any of its parts from the outside. This means the object cannot do anything or get any information from the class.

🎯 Exam Tip: This scenario highlights the importance of having at least some public members (like constructor or public methods) for a class to be functional and useful.

 

Question 23. - are the data variables that represent the features or properties of a class.
(a) Data members
(b) Member functions
(c) Both A and B
(d) None of these
Answer: (a) Data members
In simple words: Data members are like the characteristics or facts stored within a class, such as a student's name or age. They define what an object "is" or "has."

🎯 Exam Tip: Data members are essential for defining the state of an object, which is modified and accessed through member functions.

 

Question 24. - are the functions that perform specific tasks in a class.
(a) Data members
(b) Member functions
(c) Both A and B
(d) None of these
Answer: (b) Member functions
In simple words: Member functions are actions or operations that a class can perform. They describe what an object "does," like calculating a grade or displaying information.

🎯 Exam Tip: Member functions enable objects to perform actions and interact with their data members, encapsulating behavior within the class.

 

Question 25. Member functions are called as -
(a) Methods
(b) Attributes
(c) Properties
(d) None of these
Answer: (a) Methods
In simple words: In object-oriented programming, member functions are also known as methods. They are the actions that an object can take.

🎯 Exam Tip: The terms "member function" and "method" are often used interchangeably to describe functions associated with a class or object.

 

Question 26. Data members are also called as -
(a) Methods
(b) Attributes
(c) Properties
(d) None of these
Answer: (b) Attributes
In simple words: Data members are sometimes called attributes because they describe the qualities or characteristics of an object. These are the pieces of data an object holds.

🎯 Exam Tip: Understanding the synonyms for data members (attributes, fields) helps in grasping concepts across different programming languages and contexts.

 

Question 27. Classes contain a special member function called as -
(a) Constructors
(b) Destructors
(c) Both A and B
(d) None of these
Answer: (c) Both A and B
In simple words: Classes use special functions called constructors to set up new objects and destructors to clean up objects when they are no longer needed. Both are important for managing an object's life.

🎯 Exam Tip: Constructors initialize objects upon creation, while destructors release resources upon object deletion; both are essential for proper object lifecycle management.

 

Question 28. The member functions of a class can be defined in - ways.
(a) Two
(b) Three
(c) Four
(d) Five
Answer: (a) Two
In simple words: You can write the code for a member function in two main places: either directly inside the class definition, or separately outside of it. This offers flexibility in how you structure your code.

🎯 Exam Tip: Defining functions inside the class makes them inline by default, which can improve performance for small functions. External definitions are better for longer functions.

 

Question 29. The member functions of a class can be defined in - way.
(a) Inside the class definition
(b) Outside the class definition
(c) Either A or B
(d) None of these
Answer: (c) Either A or B
In simple words: Member functions can be defined either directly within the class's blueprint or separately outside of it. This choice depends on how you want to structure your code and whether you want the function to be an inline function.

🎯 Exam Tip: The choice between inline and non-inline functions often involves a trade-off between execution speed and code size. Small, frequently called functions benefit most from being inline.

 

Question 30. When a member function is defined Inside a class, it behaves like - functions.
(a) Inline
(b) General
(c) Local
(d) None of these
Answer: (a) Inline
In simple words: If you write a member function's code directly inside the class, the compiler treats it as an "inline" function. This means the function's code is copied directly where it's called, which can make the program run faster.

🎯 Exam Tip: Inline functions can reduce function call overhead, but excessive inlining can lead to larger code size. Use them for short, frequently executed functions.

 

Question 31. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at -
(a) Run Time
(b) Compile time
(c) Both A and B
(d) None of these
Answer: (b) Compile time
In simple words: When a function is "inline," its code gets pasted directly into the program at every spot it's used. This happens when the computer code is being built (compiled), not when it's actually running.

🎯 Exam Tip: Inline expansion occurs during the compilation phase, reducing function call overhead but potentially increasing the executable size if used extensively.

 

Question 32. When Member function defined outside the class, and then it is be called as - member function.
(a) Outline
(b) Non-inline
(c) Either A or B
(d) None of these
Answer: (c) Either A or B
In simple words: If a member function's code is written outside the class definition, it is referred to as an "outline member function" or a "non-inline member function." This separation helps keep the class definition clean.

🎯 Exam Tip: Non-inline functions are often used for longer, more complex member functions to improve code readability and manageability.

 

Question 33. When Member function defined outside the class using - operator.
(a) Scope resolution
(b) Membership
(c) Reference
(d) Conditional
Answer: (a) Scope resolution
In simple words: When a member function is defined outside its class, you need to tell the compiler which class it belongs to. The scope resolution operator (`::`) is used for this, linking the function back to its parent class.

🎯 Exam Tip: The scope resolution operator (`::`) is crucial for associating a function definition with its respective class when the definition is external to the class body.

 

Question 34. The class variables are called -
(a) Object
(b) Attributes
(c) Procedures
(d) None of these
Answer: (a) Object
In simple words: Variables that are created based on a class are known as objects. An object is a real-world thing that has the properties and behaviors defined by the class.

🎯 Exam Tip: An object is an instance of a class, meaning it's a concrete entity created from the class blueprint, possessing its own unique data.

 

Question 35. Objects are also called as - of class.
(a) Instant
(b) Instance
(c) Attributes
(d) None of these
Answer: (b) Instance
In simple words: An object is often called an "instance" of a class. This means it is a specific example or realization of the general blueprint provided by the class.

🎯 Exam Tip: The terms "object" and "instance" are largely interchangeable, emphasizing that an object is a specific manifestation of a class.

 

Question 36. Objects can be created in - methods.
(a) Three
(b) Four
(c) Two
(d) Five
Answer: (c) Two
In simple words: Objects can be created in two main ways: either as global objects (visible everywhere) or as local objects (visible only within a specific function). This allows for different levels of access.

🎯 Exam Tip: The two primary scopes for object creation are global (outside all functions) and local (inside a function), each having different lifetimes and visibility.

 

Question 37. Objects can be created as -
(a) Global object
(b) Local object
(c) Either A or B
(d) None of these
Answer: (c) Either A or B
In simple words: You can create objects that are either "global" (meaning they can be used anywhere in the program) or "local" (meaning they can only be used in a specific part of the program). Both ways are common depending on the need.

🎯 Exam Tip: The scope of an object (global or local) determines its visibility and lifetime within the program, which is a key design consideration.

 

Question 38. - objects can be used by any function in the program.
(a) Global object
(b) Local object
(c) Either A or B
(d) None of these
Answer: (a) Global object
In simple words: A global object is like a public tool that all parts of the program can use. It is created once and stays active throughout the program's life.

🎯 Exam Tip: While global objects offer universal access, overuse can lead to tightly coupled code and make debugging difficult, so use them judiciously.

 

Question 39. If an object is declared outside all the function bodies or by placing their names immediately after the closing brace of the class declaration then it is called as -
(a) Global object
(b) Local object
(c) Either A or B
(d) None of these
Answer: (a) Global object
In simple words: When an object is created outside of any function, or right after the class definition ends, it's called a global object. This means it can be seen and used by any part of the program.

🎯 Exam Tip: Declaring global objects can be convenient for shared data, but it's generally advised to limit their use to avoid potential side effects and maintain modularity.

 

Question 40. If an object is declared within a function then it is called -
(a) Global object
(b) Local object
(c) Either A or B
(d) None of these
Answer: (b) Local object
In simple words: An object created inside a function is called a local object. It can only be used within that specific function and disappears when the function finishes running.

🎯 Exam Tip: Local objects are good for limiting the scope and lifetime of data, which helps in managing memory and preventing unintended interactions between different parts of a program.

 

Question 41. - object can not be accessed from outside the function.
(a) Global
(b) Local
(c) Either A or B
(d) None of these
Answer: (b) Local
In simple words: A local object is like a temporary tool used only inside one specific room (function). Once you leave that room, the tool is no longer available to you.

🎯 Exam Tip: This restricted access of local objects helps in creating self-contained functions, reducing complexity and potential errors in larger programs.

 

Question 42. No separate space is allocated for - when the objects are created.
(a) Member functions
(b) Data members
(c) Both A and B
(d) None of these
Answer: (a) Member functions
In simple words: When you create objects from a class, memory is set aside for the actual data (data members), but not for the functions themselves. All objects share the same set of member functions.

🎯 Exam Tip: Member functions are stored only once in memory and are shared by all objects of the class, while each object gets its own copy of the data members.

 

Question 43. Memory space required for the -
(a) Member functions
(b) Data members
(c) Both A and B
(d) None of these
Answer: (b) Data members
In simple words: Each object created from a class needs its own space in memory to store its unique facts or characteristics, which are called data members. The functions, however, are shared.

🎯 Exam Tip: The size of an object is primarily determined by its data members, as they are duplicated for each instance, unlike member functions which are shared.

 

Question 44. The members of a class are referenced (accessed) by using the object of the class followed by the - operator.
(a) Scope resolution
(b) Conditional
(c) Dot (membership)
(d) None of these
Answer: (c) Dot (membership)
In simple words: To reach something inside an object, like its data or a function, you use a small dot (`.`) right after the object's name. This dot lets you access its parts.

🎯 Exam Tip: The dot operator (`.`) is used for direct member access when working with an object instance, enabling interaction with its encapsulated data and behavior.

 

Question 45. Calling a member function of an object is also known as -
(a) Sending a message to object
(b) Communication with the object
(c) Either A or B
(d) None of these
Answer: (c) Either A or B
In simple words: When you make an object perform one of its actions (call a member function), it's like sending a command to that object. So, it's also known as "sending a message" or "communicating with the object."

🎯 Exam Tip: This conceptual view of "message passing" is fundamental to understanding object-oriented interaction, where objects interact by invoking each other's methods.

 

Question 46. An array which contains the class type of element is called -
(a) Array of objects
(b) Structure Objects
(c) Block of objects
(d) None of these
Answer: (a) Array of objects
In simple words: When you have a list or collection where each item in the list is an object of a certain class, that collection is called an "array of objects." It's like having a list of many similar items.

🎯 Exam Tip: An array of objects allows you to manage multiple objects of the same class efficiently, treating them as a single collection.

 

Question 47. The - of the outline member function given in a class specification, instructs the compiler about its visibility mode.
(a) Name
(b) Prototype
(C) Data type
(d) None of these
Answer: (b) Prototype
In simple words: The "prototype" of a function tells the compiler important things like its name, what kind of information it takes in, and what type of result it gives back. This helps the compiler know how to use the function correctly.

🎯 Exam Tip: A function prototype (or declaration) is crucial for forward declaration, allowing functions to be called before their full definition appears in the code.

 

Question 48. A member function can call another member function of the same class directly without using the dot operator is called -
(a) Nesting of the member function
(b) Invariant Members
(c) Variant Members
(d) None of these
Answer: (a) Nesting of the member function
In simple words: When one function inside a class calls another function that is also inside the same class, without needing a dot, it's called "nesting of member functions." This is a way for functions to work together internally.

🎯 Exam Tip: Nesting member functions simplifies internal class operations by allowing functions to reuse logic from other functions within the same object.

 

Question 49. A member function can call another member function of the same class for that you do not need a(n) -
(a) Member function
(b) Data Member
(c) Object
(d) None of these
Answer: (c) Object
In simple words: When a member function needs to call another member function from the same class, it doesn't need to use an object to do so. It can simply call the other function directly. This makes internal calls easier.

🎯 Exam Tip: This internal direct calling mechanism simplifies the implementation of complex behaviors within a class, as functions can naturally build upon each other.

 

Question 50. A member function can access - functions.
(a) Public
(b) Private
(c) Both A and B
(d) None of these
Answer: (c) Both A and B
In simple words: A member function of a class can reach and use both the public and private functions within that same class. This means it has full access to other functions inside its own class.

🎯 Exam Tip: This internal access is crucial for encapsulation, as public member functions can then manage interactions with private member functions and data.

 

Question 51. - operator will reveal the hidden file scope(global) variable.
(a) Membership
(b) Conditional
(c) Scope resolution
(d) All of the above
Answer: (c) Scope resolution
In simple words: If you have a variable that's hidden because another variable with the same name exists in a closer scope, the scope resolution operator (`::`) helps you find and use the hidden (global) one. It tells the compiler to look in the outermost scope.

🎯 Exam Tip: The scope resolution operator (`::`) is essential for resolving ambiguities when local and global variables (or class members) share the same name.

 

Question 52. When an object is passed by - the function creates its own copy of the object and works on it.
(a) Value
(b) Reference
(c) Either A or B
(d) None of these
Answer: (a) Value
In simple words: If you pass an object "by value" to a function, the function gets a separate copy of that object. Any changes made to the copy inside the function will not affect the original object.

🎯 Exam Tip: Passing by value ensures that the original object remains unchanged, providing data integrity, but it can be less efficient for large objects due to copying overhead.

 

Question 53. When an object is passed by - changes made to the object inside the function do not affect the original object.
(a) Value
(b) Reference
(c) Either A or B
(d) None of these
Answer: (a) Value
In simple words: When an object is sent to a function "by value," the function receives a brand new copy. This means anything done to that copy inside the function will not change the first, original object.

🎯 Exam Tip: This behavior is crucial for functions that should operate on an object's data without altering the original state of the calling object.

 

Question 54. When an object is passed by - its memory address is passed to the function so the called function works directly on the original object used in the function call.
(a) Value
(b) Reference
(c) Either A or B
(d) None of these
Answer: (b) Reference
In simple words: When you pass an object "by reference," you are not sending a copy, but rather the actual memory location of the object. This lets the function work directly on the original object, so any changes it makes will affect the real object.

🎯 Exam Tip: Passing by reference is efficient for large objects as it avoids copying, and it allows functions to modify the original object, which is useful for 'setter' methods.

 

Question 55. When an object is passed by - any changes made to the object inside the function definition are reflected in the original object.
(a) Value
(b) Reference
(c) Either A or B
(d) None of these
Answer: (b) Reference
In simple words: If you send an object to a function "by reference," the function directly works on the original object. This means any changes made by the function will immediately show up in the original object outside the function.

🎯 Exam Tip: This mechanism is crucial when a function needs to modify the state of the object it receives, for example, in utility functions that update an object's properties.

 

Question 56. Member Functions can -
(a) Receive object as an argument
(b) Return an object
(c) Both A and B
(d) None of these
Answer: (c) Both A and B
In simple words: Member functions are very flexible; they can take objects as input when they are called, and they can also give back an object as their result. This allows for complex interactions between objects.

🎯 Exam Tip: The ability of member functions to accept and return objects enables advanced programming patterns like function chaining and object manipulation.

 

Question 57. When one class become a member of another class then it is called - class.
(a) Nested
(b) Inline
(c) External
(d) Global
Answer: (a) Nested
In simple words: When you put one class inside another class, the inner class is called a "nested class." It's like having a small box inside a bigger box.

🎯 Exam Tip: Nested classes are useful for grouping related classes and improving encapsulation, as the inner class is closely tied to its outer class.

 

Question 58. When one class becomes a member of another class then the relationship is called …………………….
(a) Containership
(b) Nesting
(c) Parent-Child
(d) None of these
Answer: (a) Containership
In simple words: When one class is included inside another class, it creates a "containership" relationship. This is like one box holding another box.

🎯 Exam Tip: Remember that 'containership' specifically refers to the relationship where one class is a member of another, which is a key concept in object-oriented design.

 

Question 59. Classes can be nested in ……………….. ways.
(a) Three
(b) Two
(c) Four
(d) Five
Answer: (b) Two
In simple words: There are mainly two ways to make classes nested, which means putting one class inside another.

🎯 Exam Tip: Understanding the different methods of nesting classes helps in organizing code and managing relationships between data structures effectively.

 

Question 60. Classes can be nested in ………………..way.
(a) By defining a class within another class
Answer: (a) By defining a class within another class
In simple words: One way to nest classes is to simply write the full definition of one class inside another class. This makes the inner class a part of the outer one.

🎯 Exam Tip: This method of nesting creates a strong logical connection, where the inner class is usually specific to the outer class's functionality.

 

Question 61. When a class is declared within another class, the inner class is called a Nested class (ie the inner class) and the outer class is known as …………………… class.
(a) Enclosing
(b) Abstract
(c) Transit
(d) None of these
Answer: (a) Enclosing
In simple words: When you put a class inside another class, the one on the outside is called the "enclosing class," and the one inside is the "nested class." The enclosing class holds the nested class.

🎯 Exam Tip: Knowing the terms "nested class" and "enclosing class" is crucial for discussing and understanding class relationships in object-oriented programming.

 

Question 62. The nested class can be defined in ………………….. section of the Enclosing class.
(a) Private
(b) Public
(c) Either Private or Public
(d) None of these
Answer: (c) Either Private or Public
In simple words: You can define a nested class in either the private or public part of its outer (enclosing) class. This controls how much other parts of your program can see or use the nested class.

🎯 Exam Tip: The choice of access specifier for a nested class (private, public, or protected) dictates its visibility and accessibility, impacting encapsulation and design.

 

Question 63. Whenever an object of a class is declared as a member of another class it is known as a _____ class.
(a) Abstract
(b) Container
(c) Literal
(d) None of these
Answer: (b) Container
In simple words: When a class has an object of another class inside it, the outer class is called a "container class." It acts like a holder for objects of other classes.

🎯 Exam Tip: Container classes are fundamental for building complex data structures and managing relationships between objects through composition.

 

Question 64. Instantiating object is done using ……………….
(a) Constructor
(b) Destructor
(c) Data abstraction
(d) Data hiding
Answer: (a) Constructor
In simple words: Creating a new object from a class is called "instantiating," and this special job is done by a "constructor." The constructor sets up the new object when it is made.

🎯 Exam Tip: Constructors are essential for initializing objects correctly and ensuring they are in a valid state from the moment they are created.

 

Question 65. A(n) ………………..in C++ can be initialized during the time of their declaration.
(a) Array
(b) Structure
(c) Array or Structure
(d) None of these
Answer: (c) Array or Structure
In simple words: In C++, you can give starting values to both arrays and structures right when you create them. This makes it easier to set them up without needing extra steps later.

🎯 Exam Tip: Initialization at declaration makes code cleaner and reduces potential errors by ensuring variables and data structures have valid starting values.

 

Question 66. Member function of a class can access all the members irrespective of their associated …………………..
(a) Access specifier
(b) Data type
(c) Return type
(d) Argument
Answer: (a) Access specifier
In simple words: A member function inside a class can always get to all the other parts of that same class, no matter if they are private, protected, or public. It doesn't matter what their access setting is.

🎯 Exam Tip: This principle of full internal access for member functions is a cornerstone of encapsulation, allowing controlled interaction with private data.

 

Question 67. When an instance of a class comes into scope, a special function called the ……………. gets executed.
(a) Constructor
(b) Destructor
(c) Data abstraction
(d) Data hiding
Answer: (a) Constructor
In simple words: When a new object (an instance of a class) is created and becomes available for use, a special starting function, called a "constructor," runs automatically to set it up.

🎯 Exam Tip: Always remember that constructors are automatically invoked upon object creation, making them ideal for initial setup and resource allocation.

 

Question 68. The constructor function name has the same name as the …………….. name.
(a) Object
(b) Class
(c) Data member
(d) None of these
Answer: (b) Class
In simple words: A constructor is a special function that always has the exact same name as the class it belongs to. This helps the computer know it's a constructor.

🎯 Exam Tip: This naming convention is a fundamental rule for constructors in C++ and many other object-oriented languages.

 

Question 69. The constructors return ………………
(a) int
(b) char
(c) float
(d) nothing
Answer: (d) nothing
In simple words: Constructors do not give back any value when they finish running. They just do their job of setting up an object.

🎯 Exam Tip: Unlike regular functions, constructors explicitly have no return type, not even 'void', as their purpose is object initialization, not value computation.

 

Question 70. …………………. are not associated with any data type
(a) Constructor
(b) Data member
(c) Data abstraction
(d) Member functions
Answer: (a) Constructor
In simple words: Constructors are special because they don't have a specific data type like "int" or "float" associated with them for returning a value. Their main job is to build an object.

🎯 Exam Tip: This characteristic distinguishes constructors from other functions and emphasizes their role in object instantiation rather than data processing.

 

Question 71. ……………… can be defined either inside class definition or outside the Class definition.
(a) Constructor
(b) Destructor
(c) Data abstraction
(d) Member functions
Answer: (a) Constructor
In simple words: You can write the code for a constructor either directly inside the class definition or separately outside of it. Both ways work to create and set up objects.

🎯 Exam Tip: Defining constructors outside the class typically uses the scope resolution operator (::) to link the definition to its class.

 

Question 72. A constructor can be defined in ……………… section of a class.
(a) Private
(b) Public
(c) Either Private or Public
(d) None of these
Answer: (c) Either Private or Public
In simple words: A constructor can be placed in either the public or private part of a class. Placing it in the public section makes it easy to create objects from anywhere.

🎯 Exam Tip: While constructors are most commonly public to allow object creation, private constructors are used for advanced patterns like singletons or when objects are created internally by static methods.

 

Question 73. If a constructor is defined in ………………… section of a class, then only its object Can be created in any function.
(a) Private
(b) Public
(c) Either Private or Public
(d) None of these
Answer: (b) Public
In simple words: If you want to create objects of a class from any part of your program, you must put the constructor in the public section. This makes it visible and usable everywhere.

🎯 Exam Tip: Public constructors allow direct instantiation of objects, which is the most common use case for classes in most programs.

 

Question 74. The main function of the constructor is ……………………….
(a) To allocate memory space to the object
(b) To initialize the data member of the class object
(c) Either A or B
(d) None of these
Answer: (c) Either A or B
In simple words: The main job of a constructor is to prepare a new object. This involves both setting aside memory for the object and giving starting values to its internal data.

🎯 Exam Tip: Constructors are crucial for ensuring objects are ready to be used as soon as they are created, handling both memory and initial state.

 

Question 75. A constructor that accepts no parameter is called …………………… constructor.
(a) Null
(b) Default
(c) Empty
(d) None of these
Answer: (b) Default
In simple words: A constructor that doesn't need any input values (parameters) when you use it is called a "default constructor." It sets up an object with standard starting values.

🎯 Exam Tip: The default constructor is important because if you don't define any constructor, the compiler automatically provides a public default constructor.

 

Question 76. Identify the correct statement from following with respect to constructor.
(a) If a class does not contain an explicit constructor (user defined constructor) the compiler automatically generate a default constructor implicitly as an inline public member.
(b) In the absence of user defined constructor the compiler automatically provides the default constructor. It simply allocates memory for the object.
(c) Parameterized constructor is achieved by passing parameters to the function.
(d) All the options
Answer: (d) All the options
In simple words: All the given statements about constructors are true. The compiler can create a default one if you don't, it handles memory, and you can give parameters to constructors.

🎯 Exam Tip: This question tests a comprehensive understanding of constructor behavior, including default generation, memory allocation, and parameterization.

 

Question 77. A constructor which can take arguments is called ……………….. constructor.
(a) Parmeterized
(b) Default
(c) Empty
(d) None of these
Answer: (a) Parmeterized
In simple words: A constructor that needs you to give it some input values (arguments) when you create an object is called a "parameterized constructor." This lets you set up an object with specific starting data.

🎯 Exam Tip: Parameterized constructors are highly useful for creating objects with diverse initial states, making your classes more flexible.

 

Question 78. ……………….. type of constructor helps to create objects with different initial values.
(a) Parmeterized
(b) Default
(c) Empty
(d) None of these
Answer: (a) Parmeterized
In simple words: Parameterized constructors are helpful because they allow you to give different starting values to different objects when you create them. This makes each object unique from the start.

🎯 Exam Tip: Utilize parameterized constructors when objects need specific, varied initial states, as opposed to the uniform initialization provided by a default constructor.

 

Question 79. Declaring a constructor with arguments hides the ……………………
(a) Data members
(b) Compiler generated constructor
(c) Member functions
(d) None of these
Answer: (b) Compiler generated constructor
In simple words: If you create your own constructor that takes inputs, the computer will not automatically make its own default constructor for you. Your custom constructor "hides" the one the compiler would have made.

🎯 Exam Tip: Remember the rule of three/five/zero: if you define any constructor, copy constructor, assignment operator, or destructor, you generally need to define all of them to prevent compiler-generated defaults from causing issues.

 

Question 80. ………………… Constructor is used to creating an array of objects.
(a) Default
(b) Parameterized
(b) Overloaded
(d) None of these
Answer: (a) Default
In simple words: To make a list (an array) of many objects, you often use a default constructor. This is because it doesn't need any special inputs for each object in the list.

🎯 Exam Tip: When declaring an array of objects, the default constructor is called for each element unless an explicit initializer list is provided.

 

Question 81. There are ………………. ways to create an object using the parameterized constructor.
(a) Three
(b) Two
(c) Four
(d) Five
Answer: (b) Two
In simple words: You can create objects using a parameterized constructor in two main ways: either implicitly (without directly calling it) or explicitly (by calling it directly).

🎯 Exam Tip: The two ways are implicit call (e.g., `MyClass obj(param);`) and explicit call (e.g., `MyClass obj = MyClass(param);`).

 

Question 82. ……………… is a way to create an object using the parameterized constructor,
(a) Implicit call
(b) Explicit call
(c) Either A or B
(d) None of these
Answer: (c) Either A or B
In simple words: You can create an object with a parameterized constructor using either an implicit call (shorter way) or an explicit call (more direct way). Both methods achieve the same result.

🎯 Exam Tip: While both implicit and explicit calls work, implicit calls are often preferred for their conciseness, especially in C++11 and later.

 

Question 83. In …………….. method, the parameterized constructor is invoked automatically whenever an object is created.
(a) Implicit call
(b) Explicit call
(c) Either A or B
(d) None of these
Answer: (a) Implicit call
In simple words: When you use the "implicit call" method, the parameterized constructor runs on its own every time a new object is made, without you having to write a direct call to it.

🎯 Exam Tip: Implicit calls are generally more elegant and concise, fitting well with the idea of objects being initialized naturally upon declaration.

 

Question 84. In the………………….. method, the name of the constructor is explicitly given to invoking the parameterized constructor.
(a) Implicit call
(b) Explicit call
(c) Either A or B
(d) None of these
Answer: (b) Explicit call
In simple words: When you use the "explicit call" method, you clearly write out the name of the constructor when you create an object. This makes it very clear that you are calling that specific constructor.

🎯 Exam Tip: Explicit calls, while sometimes more verbose, can enhance readability in complex initializations by clearly showing which constructor is being used.

 

Question 85. ………………… method is the most suitable method as it creates a temporary object
(a) Implicit call
(b) Explicit call
(c) Either A or B
(d) None of these
Answer: (b) Explicit call
In simple words: The "explicit call" method is often best for making temporary objects. These are objects that are used only for a short time, maybe just for one calculation, and then they are removed from memory.

🎯 Exam Tip: Explicit calls are frequently used when a temporary object is needed as an argument to a function or for a quick, one-off operation.

 

Question 86. The chance of data loss will not arise in ………………….. method.
(a) Implicit call
(b) Explicit call
(c) Either A or B
(d) None of these
Answer: (b) Explicit call
In simple words: When you use the "explicit call" method for creating objects, there is less risk of losing any data. This method helps to make sure all information is correctly passed and kept.

🎯 Exam Tip: Explicit calls can sometimes offer better control and transparency, which might help in preventing subtle data corruption issues in complex scenarios.

 

Question 87. A ……………….. object lives in memory as long as it is being used in an expression.
(a) Temporary
(b) Nested
(c) Inline
(d) None of these
Answer: (a) Temporary
In simple words: A "temporary object" is an object that only stays in the computer's memory for a very short time, just long enough to be used in a calculation or instruction. After that, it is removed.

🎯 Exam Tip: Temporary objects are often created by the compiler to hold intermediate results of expressions and are destroyed at the end of the full expression containing them.

 

Question 88. A constructor having a reference to an already existing object of its own class is called ………………….. constructor.
(a) Reference
(b) Value
(c) Copy
(d) Move
Answer: (c) Copy
In simple words: A special constructor that takes an existing object of the same class as input (usually by reference) to create a new, identical object is called a "copy constructor." It makes a copy of the original.

🎯 Exam Tip: The copy constructor is invoked during object initialization from another object, passing objects by value, and returning objects from a function by value.

 

Question 89. A copy constructor is called ……………..
(a) When an object is passed as a parameter to any of the member functions
(b) When a member function returns an object
(c) When an object is passed by reference to an instance of its own class
(d) All the options
Answer: (d) All the options
In simple words: A copy constructor gets used in many situations, such as when you pass an object into a function, when a function gives an object back, or when you pass an object by reference to create another object of its type.

🎯 Exam Tip: Understanding the situations that trigger a copy constructor is vital to prevent unintended object copies and manage resources effectively, especially with dynamically allocated memory.

 

Question 90. The constructors are executed in the …………………. of the object declared.
(a) Order
(b) Reverse order
(c) Either A or B
(d) None of these
Answer: (a) Order
In simple words: When you declare several objects, their constructors are always run in the same sequence in which you declared those objects. It's a first-come, first-served rule.

🎯 Exam Tip: The order of constructor execution matches the order of declaration, which is important for understanding object lifetime and dependencies.

 

Question 91. When the initial values are provided during runtime then it is called ………………….. initialization.
(a) Static
(b) Dynamic
(c) Run time
(d) None of these
Answer: (b) Dynamic
In simple words: When you give starting values to objects while the program is actually running, not before it even starts, this process is called "dynamic initialization."

🎯 Exam Tip: Dynamic initialization provides flexibility, allowing initial values to depend on user input or other runtime conditions, which is crucial for interactive programs.

 

Question 92. …………………. constructor can have parameter list.
(a) Constructor
(b) Destructor
(c) Both A and B
(d) None of these
Answer: (a) Constructor
In simple words: A constructor is the only one that can take a list of inputs (parameters) to help set up a new object with specific starting values.

🎯 Exam Tip: Only constructors are designed to accept parameters to facilitate diverse initializations; destructors, by contrast, do not take arguments.

 

Question 93. No return type can be specified for ………………….
(a) Constructor
(b) Destructor
(c) Both A and B
(d) None of these
Answer: (c) Both A and B
In simple words: Neither constructors nor destructors are allowed to specify a type of value they return. They simply perform their tasks of setting up or cleaning up objects.

🎯 Exam Tip: The absence of a return type for constructors and destructors highlights their specialized roles in object lifecycle management rather than computation.

 

Question 94. The ……………….. function can be overloaded.
(a) Constructor
(b) Destructor
(c) Both A and B
(d) None of these
Answer: (a) Constructor
In simple words: You can have many different versions of a constructor, each taking different types or numbers of inputs. This is called "overloading," and it lets you set up objects in various ways.

🎯 Exam Tip: Constructor overloading is a powerful feature that allows classes to offer multiple ways to initialize objects, catering to different creation scenarios.

 

Question 95. The compiler generates a ………………… in the absence of a user-defined.
(a) Constructor
(b) Destructor
(c) Both A and B
(d) None of these
Answer: (a) Constructor
In simple words: If you don't write your own constructor for a class, the computer automatically creates a simple, basic constructor for you. This ensures every object can still be made.

🎯 Exam Tip: The compiler-generated default constructor is a zero-argument constructor that performs default initialization for member variables.

 

Question 96. Compiler generated constructor is ………………….. member function.
(a) private
(b) protected
(c) public
(d) None of these
Answer: (c) public
In simple words: When the computer automatically creates a constructor for your class, it always makes it public. This means you can create objects of that class from anywhere in your program.

🎯 Exam Tip: The compiler-generated default constructor is public, non-explicit, and inline, making it readily available for object creation if no other constructors are defined.

 

Question 97. The ………………. is executed automatically,
(a) Constructor
(b) Destructor
(c) Both A and B
(d) None of these
Answer: (c) Both A and B
In simple words: Both constructors and destructors run by themselves without you having to call them directly. Constructors run when an object is made, and destructors run when it's removed.

🎯 Exam Tip: This automatic invocation is a key feature of object-oriented programming, ensuring proper setup and cleanup during an object's lifetime.

 

Question 98. The ……………. is executed automatically when the object is created.
(a) Constructor
(b) Destructor
(c) Both A and B
(d) None of these
Answer: (a) Constructor
In simple words: The constructor is the special function that automatically runs as soon as a new object is created. It gets the object ready for use.

🎯 Exam Tip: Think of the constructor as the object's birth ritual, where initial properties are set up before it can be fully functional.

 

Question 99. When a class object goes out of scope, a special function called the ………………. gets executed.
(a) Constructor
(b) Destructor
(c) Both A and B
(d) None of these
Answer: (b) Destructor
In simple words: When an object is no longer needed or leaves its active area in the program, a special "destructor" function automatically runs. This function cleans up any resources the object used.

🎯 Exam Tip: Destructors are crucial for preventing memory leaks and ensuring resources (like file handles or network connections) are properly released when an object is destroyed.

 

Question 100. The destructor has the same name as the class tag but prefixed with a …………………
(a) ~ (tilde)
(b) #
(c) @
(d) None of these
Answer: (a) ~ (tilde)
In simple words: A destructor has the exact same name as its class, but with a special tilde symbol (~) placed in front of it. This symbol tells the computer it's the function for cleaning up the object.

🎯 Exam Tip: The tilde (~) prefix is a unique identifier for destructors, clearly distinguishing them from constructors and other member functions.

 

Question 101. A …………………… is a special member function that is called when the lifetime of an object ends.
(a) Constructor
(b) Destructor
(c) Both A and B
(d) None of the options
Answer: (b) Destructor
In simple words: A destructor is a special cleanup function that runs automatically when an object is no longer needed in a program, helping to free up any memory or resources it used. This ensures your program does not leave behind unused memory, which is important for efficient resource management.

🎯 Exam Tip: Remember that destructors are identified by a tilde (~) prefix before the class name and perform cleanup tasks.

 

Question 102. The ………………… cannot have arguments,
(a) Constructor
(b) Destructor
(c) Both A and B
(d) None of the options
Answer: (b) Destructor
In simple words: Destructors are special functions in a class that are called automatically when an object is destroyed, and they never take any input values or arguments. This is because their job is simply to clean up, not to process any new information.

🎯 Exam Tip: While constructors can be overloaded with different arguments, destructors always have a fixed signature (no arguments) and cannot be overloaded.

 

Question 103. There can be ……………… destructor in a class.
(a) Two
(b) Three
(c) Only one
(d) Four
Answer: (c) Only one
In simple words: For any given class, you can only have one destructor. This is because a destructor's role is fixed – to clean up resources when an object is finished – so there is no need for multiple versions.

🎯 Exam Tip: Unlike constructors, which can be overloaded, a class can only have one destructor, which must have a specific signature (no arguments or return type) to ensure consistent cleanup.

 

Question 104. _____ cannot be inherited.
(a) Constructor
(b) Destructor
(c) Both A and B
(d) None of the options
Answer: (c) Both A and B
In simple words: Neither constructors nor destructors can be inherited by child classes. However, when a child class object is created or destroyed, the parent class's constructor and destructor are still called automatically to handle its part of the object.

🎯 Exam Tip: Remember that while constructors and destructors are not inherited, they are automatically invoked when derived class objects are created and destroyed, ensuring proper initialization and cleanup across the inheritance hierarchy.

 

Very Short Answer (2 Marks)

 

Question 1. Define methods of a class and write its types.
Answer: Methods of a class are functions that perform specific tasks or actions within that class. They are also known as member functions. These methods can be defined in two main ways:
1. Inside the class definition: These are usually short functions and are automatically treated as inline functions by the compiler.
2. Outside the class definition: These are defined separately using the scope resolution operator (`::`) and are generally used for longer or more complex functions.
In simple words: Methods are like actions a class can do. You can write these actions either directly inside the class or separately outside it.

🎯 Exam Tip: When defining methods outside the class, always use the scope resolution operator (`::`) to link the method back to its class, which is crucial for proper compilation.

 

Question 2. Why classes are needed?
Answer: Classes are important because they let us create custom data types that represent real-world things. These custom data types can hold not only different kinds of data (like numbers or text) but also have their own actions (functions) linked to that data. This helps us organize our code better and model complex problems in a clear way, making programs easier to understand and manage.
In simple words: Classes help us make new types of data that can store information and also do things, just like real objects. This makes programming much more organized.

🎯 Exam Tip: Emphasize that classes serve as blueprints for objects, enabling data encapsulation and defining both data (attributes) and behavior (methods) for a unified representation of real-world entities.

 

Question 3. What is called as nesting of member functions?
Answer: Nesting of member functions happens when one member function of a class calls another member function of the *same* class directly. It means you don't need to use the dot operator (`.`) or an object name to make the call. This simplifies internal communication within a class, making the code cleaner and more efficient.
In simple words: Nesting means one function inside a class calls another function from the same class without needing an object or a dot in between.

🎯 Exam Tip: When discussing nested member functions, highlight that they can only call *other* member functions of the *same* object without the dot operator, not functions from external objects or other classes.

 

Question 4. What are the visibility labels of a class body?
Answer: The visibility labels (also known as access specifiers) for a class body define who can access the members (data and functions) of that class. There are three main types:
1. `private`: Members declared here can only be accessed by functions within the same class.
2. `public`: Members declared here can be accessed from anywhere, both inside and outside the class.
3. `protected`: Members declared here can be accessed by functions within the same class and by functions in any derived (child) classes.
In simple words: Visibility labels in a class tell us who can see and use its parts. They are private (only inside the class), public (from anywhere), and protected (inside the class and by child classes).

🎯 Exam Tip: Clearly distinguish between `private`, `public`, and `protected` by explaining their access scopes, as this is fundamental to understanding encapsulation in OOP.

 

Question 5. What is a parameterized constructor?
Answer: A parameterized constructor is a special function in a class that takes one or more input values (parameters) when an object is created. Its main purpose is to set the initial values for the object's data members right when the object comes into existence. This allows objects to be created with different starting states, making them more flexible.
In simple words: A parameterized constructor is a special function that takes values when an object is made, using them to set up the object's starting data.

🎯 Exam Tip: Remember that parameterized constructors allow objects to be initialized with different values at creation, which is key for creating diverse instances of a class.

 

Question 6. What happened if all the members of a class are defined as private?
Answer: If all members (both data and functions) of a class are defined as private, the class effectively becomes "frozen" or unusable from the outside. This means that no object created from this class would be able to access any of its data or call any of its functions directly from outside the class. It severely limits the usefulness of the class, as there would be no way to interact with its state or behavior externally.
In simple words: If everything in a class is private, nothing outside the class can use it. The class's objects cannot be interacted with from the outside.

🎯 Exam Tip: Explain that making all members private enforces strict encapsulation, which is usually counterproductive as it prevents any interaction with the object from outside its own definition.

 

Question 7. Write about objects.
Answer: Objects are specific instances of a class, created from its blueprint. A class defines the general properties and behaviors, while an object is a concrete entity that possesses those properties and can perform those behaviors. For example, if "Car" is a class, then "myRedCar" and "yourBlueCar" would be objects. Objects are fundamental to Object-Oriented Programming as they allow us program with real-world entities.
For example: `student s;`
In the above statement, 's' is an instance or object of the class 'student'.
In simple words: Objects are like real-life items made from a class's plan. They are the actual things you use in your program that have data and can do actions.

🎯 Exam Tip: Differentiate clearly between a class (the blueprint) and an object (the actual instance), often using a real-world analogy like a cookie cutter (class) and cookies (objects).

 

Question 8. How many ways objects can be created for a class? Give its types.
Answer: Objects for a class can be created in two main ways, depending on where they are declared in the program's scope:
1. **Global object:** Declared outside all functions, accessible from anywhere in the program.
2. **Local object:** Declared inside a function, accessible only within that specific function.
Each type serves different purposes in managing data and behavior within a program.
In simple words: You can create objects in two ways: globally, so they can be used anywhere, or locally, so they can only be used inside one specific function.

🎯 Exam Tip: Distinguish between global and local objects by their scope and lifetime, explaining how this impacts their accessibility and memory management.

 

Question 9. What do you mean by an array of objects?
Answer: An array of objects is simply a collection where each element is an object of a particular class. Instead of storing basic data types like integers or characters, it stores multiple instances of a class. This allows you to manage many similar objects together, much like an array of numbers but for complex data structures.
Example:
`class stock`
`{`
`int itemno;`
`float price;`
`public:`
`}`s[5];
Here `s[5]` is an array of objects.
In simple words: An array of objects is a list where each item in the list is a complete object from a class, rather than just a simple number or letter.

🎯 Exam Tip: Remember that an array of objects allows you to handle multiple objects of the same class efficiently, much like a list of simple variables.

 

Question 10. What is a nested member function?
Answer: A nested member function refers to a situation where one member function within a class calls another member function of the *same* class directly. This call happens without needing to use an object name and the dot operator, as it’s an internal call within the object itself. This approach helps organize logic and promotes modularity within a class.
In simple words: A nested member function means one function inside a class calls another function from the same class without using an object or a dot.

🎯 Exam Tip: Focus on the direct calling mechanism (without dot operator) and the "same class" restriction when explaining nested member functions.

 

Question 11. How many ways objects can be passed to function argument?
Answer: Objects can be passed to function arguments in two primary ways in C++:
* **Pass By Value:** A copy of the object is created and passed to the function. Any changes made to the object inside the function do not affect the original object outside.
* **Pass By Reference:** The memory address of the original object is passed. This means the function works directly on the original object, and any changes made inside the function will affect the original object.
These methods offer flexibility in controlling how objects are used and modified within functions.
In simple words: You can give objects to functions in two ways: either you send a copy of the object, so the original stays safe, or you send a link to the original, so the function can change it directly.

🎯 Exam Tip: Clearly differentiate between "pass by value" (creates a copy, original unchanged) and "pass by reference" (uses original, changes reflected) when explaining object passing mechanisms.

 

Question 12. What is a container class?
Answer: A container class is a class that holds objects of other classes as its members. This relationship is often called "containership" or "composition." Essentially, one class acts as a container, managing and storing objects of other classes. This allows for building complex systems by combining simpler, independent components.
In simple words: A container class is like a box that holds and manages objects from other classes. It lets you combine different parts into a larger, organized whole.

🎯 Exam Tip: Explain that containership is a "has-a" relationship, meaning the container class "has a" member object of another class, which is a key concept in object composition.

 

Question 13. What is the need for a constructor in a class?
Answer: Constructors are essential in a class because they automate the process of setting up objects when they are created. Their main job is to initialize an object's data members with proper starting values. Without constructors, objects would be created with unpredictable or garbage values, leading to errors. They ensure that every new object is in a valid and ready-to-use state from the moment it is formed.
In simple words: Constructors are needed to give an object its first values as soon as it is made, so it starts correctly and is ready to be used.

🎯 Exam Tip: Highlight that constructors ensure objects are always created in a valid state, preventing undefined behavior and simplifying object initialization.

 

Question 14. What are the functions of a constructor?
Answer: The primary functions of a constructor are:
* To allocate memory space for the object's data members when the object is created.
* To initialize the data members of the newly created class object with initial values, ensuring the object starts in a valid state.
Constructors are vital for setting up an object's initial state correctly and efficiently.
In simple words: Constructors mainly help create space for the new object and then fill its parts with starting values right away.

🎯 Exam Tip: Remember these two core functions: memory allocation and data member initialization, as they are crucial for an object's lifecycle.

 

Question 15. What is a default constructor?
Answer: A default constructor is a constructor that takes no parameters (arguments). If you don't define any constructors for your class, the C++ compiler automatically generates a public, inline default constructor. This constructor is used to create objects without specifying any initial values, similar to how basic variables are declared.
Example:
`int num; //ordinary variable declaration`
`Data d1; // object declaration`
If a class does not contain an explicit constructor, the compiler automatically generates a default constructor implicitly as an inline public member.
In simple words: A default constructor is a special setup function for a class that does not need any input. If you don't make one, the computer will create a basic one for you automatically.

🎯 Exam Tip: Note that if you define *any* constructor (even a parameterized one), the compiler will *not* automatically generate a default constructor, so you might need to define one explicitly if you still want to create objects without arguments.

 

Question 16. What is the significance of default constructor?
Answer: The default constructor is important because it allows objects to be created without needing to provide specific initial values. This is especially useful for creating arrays of objects, where each object can be initialized to a default state, and then later modified as needed. It ensures that even if no specific initialization is given, objects are still created in a predictable, blank, or standard state.
In simple words: A default constructor is important because it lets us make objects without having to give them specific starting values, which is helpful when making many objects at once.

🎯 Exam Tip: Emphasize that default constructors are crucial for simple object creation and especially for initializing arrays of objects, providing a baseline state for each instance.

 

Question 17. How many ways a constructor can be invoked?
Answer: A constructor, especially a parameterized constructor, can be invoked in two main ways to create an object:
1. **Implicit call:** The object is declared, and the parameters are passed in parentheses right after the object name. The compiler implicitly calls the constructor. Example: `MyClass obj(10, 20);`
2. **Explicit call:** The constructor name is explicitly used as part of the object creation, often resembling a function call. Example: `MyClass obj = MyClass(10, 20);`
Both methods achieve the same result: creating and initializing an object.
In simple words: Constructors can be called in two ways: either automatically when you declare an object with values, or by writing out the constructor's name like a function.

🎯 Exam Tip: Be aware that both implicit and explicit calls achieve the same goal, but the implicit call is generally preferred for its cleaner syntax unless specific situations require explicit construction.

 

Question 18. What is copy constructor?
Answer: A copy constructor is a special type of constructor that creates a new object by initializing it with an existing object of the same class. It takes a reference to an object of its own class as an argument. Its primary role is to make an exact copy of an object, ensuring all data members are duplicated correctly. This is particularly important for objects with dynamically allocated memory to avoid issues like shallow copies.
In simple words: A copy constructor makes a new object that is an exact duplicate of an existing object, helping to create identical copies safely.

🎯 Exam Tip: Remember that a copy constructor is invoked when an object is passed by value, returned by value, or explicitly initialized with another object of the same type. Deep copying is often a key concern here.

 

Question 19. What is the order of constructor invocation?
Answer: When multiple objects are declared, their constructors are executed in the order in which those objects are declared in the code. If multiple objects are declared within the same statement, the constructors are typically called from left to right. This ensures a predictable initialization sequence for objects within a program.
For example:
`Test t1;`
`Test t2; // the order of constructor execution is first for t1 and then for t2.`
In simple words: Constructors run in the same order that objects are listed or declared in your code. If several are on one line, they usually run from left to right.

🎯 Exam Tip: For objects in the same scope, constructors are invoked in declaration order. For base and derived classes, the base class constructor is always invoked before the derived class constructor.

 

Question 20. What do you mean by dynamic initialization of Object?
Answer: Dynamic initialization of an object means that the initial values for the object's data members are provided and set during the program's runtime, rather than being fixed at compile time. This allows for greater flexibility, as the object's initial state can depend on user input, calculations, or other factors determined while the program is running.
In simple words: Dynamic initialization means you give an object its starting values while the program is running, not fixed beforehand, making it more adaptable.

🎯 Exam Tip: Contrast dynamic initialization (runtime values) with static or compile-time initialization (fixed values) to highlight its flexibility in adapting to varying program conditions.

 

Question 21. Write a note on the destructor.
Answer: A destructor is a special member function in a class that is automatically called when a class object goes out of its scope or is explicitly destroyed. Its primary role is to perform cleanup operations, such as freeing up any memory or resources that the object acquired during its lifetime. Destructors have the same name as the class but are prefixed with a tilde (`~`), they do not take any arguments, and they do not have a return type.
* When a class object goes out of scope, a special function called the destructor gets executed.
* The destructor has the same name as the class tag but prefixed with a ~ (tilde).
* The destructor function also returns nothing and it does not associate with any data type.
In simple words: A destructor is a cleanup function that runs when an object is no longer needed, freeing up its resources. It has a special name (class name with a ~), takes no inputs, and gives no output.

🎯 Exam Tip: Remember the key characteristics of a destructor: tilde prefix, no arguments, no return type, and its automatic invocation for resource deallocation.

 

Question 22. What is the need for a destructor in a class?
Answer: Destructors are crucial in a class because they ensure that resources allocated to an object during its lifetime are properly released when the object is no longer needed. This includes freeing dynamic memory, closing file handles, or releasing network connections. Without destructors, programs could suffer from memory leaks and resource exhaustion, leading to instability or crashes. They complete the object's lifecycle by tidying up its allocated resources.
In simple words: Destructors are needed to clean up an object's resources, like memory, when it's finished, preventing the program from leaving a mess and running out of resources.

🎯 Exam Tip: Emphasize that destructors are vital for preventing memory leaks and ensuring proper resource management by cleaning up after an object's lifecycle ends.

 

Question 23. Define destructor.
Answer: A destructor is a special member function of a class that is automatically called when an object's lifetime ends. Its primary purpose is to release any resources (like memory, files, or network connections) that the object might have acquired during its existence. Destructors are identified by a tilde (`~`) before the class name, take no arguments, and have no return type. They are normally declared under the public visibility section of a class to ensure they can be called when needed.
In simple words: A destructor is a special function that automatically runs to clean up an object and release its resources when the object is no longer used.

🎯 Exam Tip: Highlight the `~` prefix, lack of arguments, and no return type as defining features of a destructor, and its role in automatic resource management.

 

Short Answers (3 Marks)

 

Question 1. Explain the local object with an example.
Answer: A local object is an instance of a class that is declared inside a function or a block of code. Its scope is limited to that specific function or block, meaning it can only be accessed and used within that area. Once the function or block finishes executing, the local object is automatically destroyed.
It cannot be accessed from outside the function.
cpp
#include
using namespace std;
class add //Global class
{
int a,b;
public:
int sum;
void getdata()
{
a = 5; b = 10;
sum = a + b;
}
} a1;
// add a2; // If a2 were global, it would be declared here.
int main()
{
add a3; // a3 is a local object within main()
a1.getdata(); // Global object accessed
// a3.getdata(); // This would be valid for a local object
}

In simple words: A local object is created inside a function and can only be used there. It disappears when the function finishes.

🎯 Exam Tip: When defining local objects, ensure their use is confined to their declared scope, remembering they are automatically destroyed once that scope exits.

 

Question 2. Write about private, protected, and public members of a class.
Answer: The private, protected, and public keywords are access specifiers that control the visibility and accessibility of a class's members (data variables and functions).
**The Public Members:** A public member can be accessed from anywhere in the program, both inside and outside the class. This makes them suitable for interfaces that interact with other parts of the program.
**The Private Members:** A private member can only be accessed by other members (functions) of the same class. By default, all members of a class are private if no access specifier is specified. Private members are used to hide implementation details and protect data.
**The Protected Members:** A protected member is similar to a private member, meaning it can be accessed by members of the same class. Additionally, it can also be accessed by members of derived classes (child classes) that inherit from the base class.
In simple words: Public members are for everyone to use, private members are only for the class itself, and protected members are for the class and its child classes. These rules control who can see and use the parts of a class.

🎯 Exam Tip: Clearly define the access scope for each specifier: `public` (anywhere), `private` (only within the class), and `protected` (within the class and its derived classes), as this is a core OOP concept.

 

Question 3. What is a constructor?
Answer: A constructor is a special member function of a class that is automatically executed whenever an object of that class is created. Its main purpose is to initialize the object's data members with appropriate starting values, ensuring the object is in a valid state from the beginning. Constructors have the same name as their class, do not have a return type, and can be overloaded to accept different parameters.
The definition of a class only creates a new user-defined data type. The instances of the class type should be instantiated (created and initialized). Instantiating objects is done using the constructor. An array or a structure in C++ can be initialized during the time of their declaration.
The initialization of a class type object at the time of declaration similar to a structure or an array is not possible because the class members have their associated access specifiers (private or protected or public). Therefore Classes include special member functions called constructors. The constructor function initializes the class object.
In simple words: A constructor is a special function that runs by itself when a new object is made. Its job is to set up the object with its first values, making sure it starts correctly.

🎯 Exam Tip: Remember that constructors are automatically invoked upon object creation and are essential for proper initialization, preventing objects from starting with undefined values.

 

Question 4. Explain memory allocation of objects.
Answer: When objects are created, memory is primarily allocated for their data members. All objects of the same class share the same member functions, so no separate memory space is allocated for member functions for each object. Instead, each object gets its own unique memory for its member variables because these variables hold different values for different objects. This design ensures efficient use of memory by avoiding redundant storage of function code.
Memory for Objects for p1 and p2 is illustrated:

P1 objectP2 object
CodeQuantityPriceCodeQuantityPrice
4 bytes4 bytes4 bytes4 bytes4 bytes4 bytes
12 bytes12 bytes

In simple words: Each object gets its own space in memory for its unique data, but all objects of the same class share the same code for their functions. This saves memory.

🎯 Exam Tip: Remember that memory is allocated per object only for data members, as member functions are shared to optimize memory usage.

 

Question 5. Explain the default constructor with an example.
Answer: A default constructor is a special function in a class that takes no arguments. If you don't create any constructors yourself, the C++ compiler automatically provides a public default constructor. This constructor is used to initialize objects with default values or no values, much like how ordinary variables are declared. This ensures that an object is created in a predictable state even without explicit initialization.
Example:
`int num; // ordinary variable declaration`
`Data d1; // object declaration`
In a class called `Data`, `Data::Data()` would be the default constructor. Using this, objects are created similarly to variables of other data types. If a class does not contain an explicit constructor, the compiler automatically generates a default constructor implicitly as an inline public member.
In simple words: A default constructor is a basic setup function that starts an object with no specific input. If you don't write one, the computer makes a simple one for you.

🎯 Exam Tip: A key point for default constructors is that if *any* other constructor is defined, the compiler will *not* provide the default one automatically, so you may need to define it manually.

 

Question 6. How will you refer members of the class? Give its syntax and an example.
Answer: To refer to or access members (both data and functions) of a class from outside the class, you typically use the object of that class followed by the dot (`.`) operator, and then the name of the member. This operator links the member to a specific object.
The general syntax for calling a member function is:
`Object_name.function_name(actual parameter);`
For example, consider the following illustration: Stud . execute(); Member function Dot operator Object name
In simple words: To use something inside a class, you first need an object of that class, then use a dot, and then the name of what you want to use. This dot connects the object to its specific part.

🎯 Exam Tip: The dot (`.`) operator is fundamental for accessing public members of a class through an object, so always include it in your examples and explanations.

 

Question 7. Explain the different methods of passing an object to the function argument.
Answer: When you pass an object to a function, there are two primary methods to choose from, each affecting how the object is handled within the function:
**Pass By Value:**
When an object is passed by value, the function receives its own separate copy of the object. Any changes made to this copy inside the function will not affect the original object outside the function. This method ensures that the original object remains unchanged, but it can be less efficient for large objects as it involves copying the entire object.
**Pass By Reference:**
When an object is passed by reference, its memory address is sent to the function instead of a copy. This means the function works directly on the original object in memory. Any changes made to the object within the function will directly modify the original object. This method is more efficient for large objects as it avoids copying.
In simple words: You can pass objects to functions in two ways: "by value" creates a copy so the original stays safe, or "by reference" sends a link to the original, allowing the function to change it directly.

🎯 Exam Tip: Clearly state that pass by value protects the original object by working on a copy, while pass by reference allows direct modification of the original, which is more efficient for large objects.

 

Question 8. Write about constructor.
Answer: A constructor is a special member function of a class that is automatically called when an object of that class is created. Its primary purpose is to initialize the object's data members, ensuring the object starts in a valid and ready-to-use state. Constructors share the same name as the class, do not have a return type, and cannot be inherited. They can be defined either inside the class definition (making them inline by default) or outside the class definition using the scope resolution operator. Constructors are fundamental for setting up objects correctly.
In simple words: A constructor is a special function that runs when you create an object. It sets up the object's first values, has the same name as the class, and doesn't return anything.

🎯 Exam Tip: Emphasize that constructors are automatically invoked upon object creation and are crucial for proper initialization, preventing objects from starting with undefined values.

 

Question 9. What is a parameterized constructor?
Answer: A parameterized constructor is a type of constructor that accepts one or more arguments (parameters) when an object is created. These parameters are used to initialize the object's data members with specific values provided at the time of creation. This allows you to create objects with different initial states, providing greater flexibility compared to default constructors.
Parameterized Constructors: A constructor that takes arguments is called a parameterized constructor. This type of constructor helps to create objects with different initial values. This is achieved by passing parameters to the function.
Example:
`class simple`
`{`
`private:`
`int a,b;`
`public:`
`simple(int m, int n)`
`{`
`a= m ;`
`b= n;`
`cout<<β€œ\n Parameterized Constructor of class-simple`
`}`
`};`
In simple words: A parameterized constructor is a setup function for objects that takes specific values as input when the object is made, allowing each new object to start with unique data.

🎯 Exam Tip: Highlight that parameterized constructors enable flexible object initialization, allowing different objects of the same class to have distinct starting states.

 

Question 10. What do you mean by the implicit and explicit call of a constructor?
Answer: When creating objects with parameterized constructors, there are two ways to invoke the constructor:
**Implicit call:** In this method, the constructor is called automatically by the compiler. You declare the object and provide the arguments in parentheses, similar to a regular function call, but without explicitly mentioning the constructor's name.
For example, `simple s1(10,20);` for creating the object `s1` automatically invokes the parameterized constructor.
**Explicit call:** In this method, you explicitly state the constructor's name when creating the object, treating it more like a direct function call. This clearly indicates that a constructor is being used for initialization.
For example: `simple s1 = simple(10,20); //explicit call`
Both methods result in the creation and initialization of an object using the parameterized constructor.
In simple words: An implicit call means the computer automatically calls the constructor when you make an object with values. An explicit call means you directly write the constructor's name to make and set up the object.

🎯 Exam Tip: Remember that implicit calls are often cleaner for object creation, while explicit calls can sometimes be used for clarity or in specific contexts like creating temporary objects.

 

Question 11. When copy constructor Is executed? Give examples.
Answer: A copy constructor is used in these situations:
When an object is sent as a parameter to any of the member functions. For example: `void simple::putdata(simple x);`
When a member function gives back an object. For example: `simple get data( ) { }`
When an object is passed by reference to an instance of its own class. For example: `simple1, s2(s1); // s2(s1) calls copy constructor`. This helps create new objects that are copies of existing ones, which can be useful for backup or transformation processes.
In simple words: A copy constructor runs when you make a new object that is a copy of an old one, or when an object is passed into or returned from a function.

🎯 Exam Tip: Remember that a copy constructor takes a reference to an object of the same class as its argument. Understand these three scenarios well, as they are common application points for copy constructors.

Explain In Detail (5 Marks)

 

Question 1. Explain how to define class members?
Answer: A class is made up of members. These members are of two types: data members and member functions.
Data members are the variables that store information, representing the features or properties of a class.
Member functions are the routines or blocks of code that perform specific tasks within a class. These are also known as methods, and data members are sometimes called attributes. The way these members are defined greatly impacts how a class functions.

Defining methods of a class:
Without defining the methods (functions), a class definition would be incomplete. Member functions of a class can be defined in two ways:

  • Inside the class definition
  • Outside the class definition

Inside the class definition:
When a member function is defined directly inside a class, it acts like an inline function. These are called inline member functions.

Outside the class definition:
When a member function is defined outside the class, just like a regular function definition, it is called an outline member function or a non-inline member function. The scope resolution operator `(::)` is used for this purpose. A common practice for larger projects is to declare functions inside the class and define them outside for better code organization.

The syntax for defining an outline member function is:
`return_type class_name :: function_name (parameter list)`
`{`
` function definition`
`}`

Class using Inline and Outline member function:
`#include`
`using namespace std;`
`class Box`
`{`
`// no access specifier mentioned`
`double width;`
`public:`
`double length;`
`//inline member function definition`
`void printWidth( )`
`{`
`cout<<"\\n The width of the box is..."<`}`
`//prototype of the function`
`void setWidth(double w);`
`};`
`// outline member function definition`
`void Box :: setWidth(double w)`
`{`
`width=w;`
`}`
`int main( )`
`{`
`// object for class Box`
`Box b;`
`// Use member function to set the width.`
`b.setWidth(10.0);`
`//Use member function to print the width.`
`b.printWidth( );`
`return 0;`
`}`

Output
`The width of the box is... 10`
In simple words: Class members can be variables (data members) or functions (member functions). Functions can be defined either inside the class (inline) or outside (outline), with the latter using a special `::` operator.

🎯 Exam Tip: Clearly differentiate between data members and member functions. When writing code, pay close attention to the scope resolution operator `(::)` for functions defined outside the class, as it's crucial for correct compilation.

 

Question 2. What are the ways to create an object using the parameterized constructor with an example?
Answer: There are two main ways to create an object when using a parameterized constructor:
1. Implicit call: In this method, the parameterized constructor is called automatically when an object is created. For example, `simple s1(10,20);` directly calls the constructor with the given values. This approach makes the code cleaner and easier to read, as the constructor is inferred.

2. Explicit call: In this method, the name of the constructor is clearly stated to call the parameterized constructor. This ensures the object is created and initialized with the specified values. For example: `simple s2 = simple(30,45);`.

Here is a program example to show both methods:
`#include`
`using namespace std;`
`class simple`
`{`
`private:`
`int a, b;`
`public:`
`simple(int m,int n)`
`{`
`a = m;`
`b = n;`
`cout << "\\n Constructor of class – simple invoked for implicit and explicit call" << endl;`
`}`
`void putdata()`
`{`
`cout << "\\n The two integers are..." << a << '\\t' << b << endl;`
`cout << "\\n The sum of the variables" << a << "+" << b << "=" << a + b;`
`}`
`};`
`int main()`
`{`
`simple s1(10,20); //implicit call`
`simple s2 = simple(30,45); //explicit call`
`cout << "\\n\\t\\tObject 1\\n";`
`s1.putdata();`
`s2.putdata();`
`return 0;`
`}`

Output:
`Constructor of class – simple invoked for the implicit and explicit call`
`Constructor of class-simple invoked for the implicit and explicit call`
` Object 1`
` The two integers are... 10 20`
` The sum of the variables 10 + 20 = 30`
` The two integers are... 30 45.`
` The sum of the variables 30 + 45 = 75`
In simple words: You can create objects using a parameterized constructor in two ways: implicitly (just by giving values in parentheses) or explicitly (by saying `simple(...)` to clearly call the constructor).

🎯 Exam Tip: Remember to use the correct syntax for both implicit and explicit calls. The explicit call can be more readable for complex initializations, while implicit calls are concise.

 

Question 3. What are the characteristics of a destructor?
Answer: Destructors are special member functions in a class with distinct characteristics:

  • The destructor has the same name as the class, but it is prefixed with a tilde character `~` (e.g., `~MyClass`).
  • Destructors cannot accept any arguments.
  • They do not have a return type, not even `void`.
  • Destructors cannot be overloaded, meaning there can only be one destructor in a class.
  • If a user-defined destructor is not provided, the compiler automatically generates a default one.
  • The destructor is automatically executed when the program control reaches the end of the class scope, or when an object is explicitly deleted, to destroy the object and free up resources. This helps prevent memory leaks.
  • Destructors cannot be inherited by derived classes.

In simple words: A destructor is like a cleanup function for a class object. It has the same name as the class but with a `~` in front, takes no inputs, and runs automatically when an object is no longer needed to free up its memory.

🎯 Exam Tip: The `~` symbol is the key identifier for a destructor. Always remember that destructors cannot be overloaded and do not take arguments, which makes them very specific in their role.

Evaluate Yourself

 

Question 1. Define a class in general and in C++’s context.
Answer: In general, a class is a blueprint or a template for creating objects. It defines the properties (data) and behaviors (functions) that objects of that type will have. It's like a plan for building houses – each house is an object, and the blueprint is the class.
In C++, a class is a user-defined data type that groups related data members and member functions into a single unit. It acts as a way to bind the data and its associated functions together, which is a fundamental concept in Object-Oriented Programming. For instance, a `Car` class might have `color` and `speed` as data members and `accelerate()` and `brake()` as member functions.
In simple words: A class is a plan for making things (objects). In C++, it lets you group related information and actions into one custom data type.

🎯 Exam Tip: Emphasize that a class is a "blueprint" and a "user-defined data type" in C++, as these are core definitions. Mentioning both data members and member functions shows a complete understanding.

 

Question 2. What is the purpose of a class specifier?
Answer: The main purpose of class specifiers (also known as access specifiers or visibility labels) is to control access to the members of a class. This concept is a core feature of Object-Oriented Programming called data hiding, which prevents functions outside the class from directly accessing or modifying its internal data. This protects the data from unintended changes.
In C++, the access restriction to class members is set using `public`, `private`, and `protected` sections within the class body. Each specifier dictates which parts of a program can see and use the class members declared under it.
In simple words: Class specifiers like `public` or `private` control who can see and use the parts inside a class. They help keep sensitive data safe from outside changes.

🎯 Exam Tip: When discussing class specifiers, always link them to "data hiding" and "access control." Listing the three main specifiers (`public`, `private`, `protected`) is essential for a complete answer.

 

Question 3. Compare a structure and a class in C++ context.
Answer: In C++, both structures and classes are user-defined data types that can hold both data members and member functions. The key difference between them lies in their default member access.
The members of a structure are `public` by default, meaning they can be accessed from anywhere outside the structure. On the other hand, the members of a class are `private` by default, meaning they can only be accessed by member functions of that same class. This default privacy in classes promotes data hiding, a fundamental principle of OOP.
In simple words: Both classes and structures group data and functions. The main difference is that a structure's parts are open to everyone by default, while a class's parts are hidden by default.

🎯 Exam Tip: The default access specifier is the critical distinction between `struct` and `class` in C++. Highlighting `public` for `struct` and `private` for `class` will earn full marks.

 

Question 4. Compare private and public access specifier.
Answer: Private and public are two fundamental access specifiers in C++ that determine the visibility and accessibility of class members.
Public members:
A public member is accessible from anywhere outside the class but still within the program. This means other functions or objects can directly interact with public data members and call public member functions. We can directly set and get the value of public data members without needing another member function. Public members form the interface through which users interact with the class.
Private members:
A private member cannot be accessed directly from outside the class. Only the member functions of that same class can access its private members. By default, if no access specifier is mentioned, all members of a class are private. This promotes encapsulation and data hiding, preventing external code from tampering with the internal state of an object. For example, sensitive internal data is usually kept private.
In simple words: Public members are visible and usable by anyone outside the class, like a car's steering wheel. Private members are hidden and only used by the class's own internal functions, like the engine parts.

🎯 Exam Tip: Clearly define "accessible from anywhere" for public and "only by class members" for private. Connect private members directly to data hiding and encapsulation, which are key OOP concepts.

 

Question 5. What is a non-inline member function? Write its syntax.
Answer: A non-inline member function is a function defined outside the class definition itself, much like a regular standalone function. When a member function is defined in this way, it is referred to as an outline member function or a non-inline member function. This method is often used for larger functions to keep the class definition clean and focused on declarations. The scope resolution operator `(::)` is used to link the function definition back to its class.

The syntax for defining an outline member function is:
Syntax:
`return_type class_name :: function_name (parameter list)`
`{`
` function definition`
`}`
In simple words: A non-inline member function is a function of a class that is written outside the class's main block. You use `::` to show which class it belongs to.

🎯 Exam Tip: The crucial element of a non-inline member function is the use of the scope resolution operator `(::)`. Make sure to include this in the syntax and explain its purpose clearly.

Activity – 1

 

Question. State the reason for the invalidity of the following code fragment.
`(i) (ii)`
`class count`
`{`
`int first`
`int second;`
`public:`
`int first`
`};`

`class item`
`{`
`int prd;`
`};`
`int prdno;`
Answer: Here are the reasons why the given code fragments are invalid:

  • In the `class count` code, the data member `first` is duplicated. It is defined twice, once implicitly as private (due to default access) and again explicitly as public. This leads to an error because a member cannot have two different declarations within the same class. Each member must have a unique name within its scope.
  • In the `class item` code, `int prdno;` is declared immediately after the closing brace of `class item` without a semicolon, and it also appears to be a global variable or an incorrectly placed data member. If `prdno` is meant to be a member of `class item`, it should be declared inside the class definition. If it's a global variable, it should have a semicolon after the class definition. The object name prefix with only the class name is not allowed without a data type in between class name and object name. The line `int prdno;` itself is syntactically correct as a global variable, but its context here (following a class definition without a semicolon) makes it questionable or potentially misleading. It's important to be clear about its intended scope and proper placement.

In simple words: The first code has a variable named `first` defined two times, which is not allowed. The second code places `prdno` wrongly; it should either be inside the class or have a semicolon after the class definition if it's a global variable.

🎯 Exam Tip: For class definitions, ensure unique member names and correct placement of global variables. Always check for proper syntax, especially semicolons after class definitions and correct scope declarations.

Activity – 2

 

Question. Write an outline function definition for calc( ); which finds the area of a square
`class area`
`{`
`int s;`
`public:`
`void calc( );`
`};`
Answer: Here is the outline function definition for `calc()` to find the area of a square:
`int area :: calc( )`
`{`
`return(s * s);`
`}`
In simple words: To find the area, the `calc()` function multiplies the side `s` by itself. The `area::` part tells the computer that this `calc()` function belongs to the `area` class.

🎯 Exam Tip: Remember to use the scope resolution operator `(::)` when defining a member function outside its class. The return type and function signature must exactly match the declaration inside the class.

Activity – 3

 

Question. Identify the error in the following code fragment
`class A`
`{`
`float x;`
`void init( )`
`{`
`Aa1;`
`X1.5=1; .`
`}`
`};`
`void main( )`
`{`
`A1.init( );`
`}`
Answer: Error:
The main error is that the local object `A a1;` declared inside the `init()` member function cannot be accessed from outside that function, specifically not from `main()`. `A1.init();` in `main()` is trying to call `init()` on a global object named `A1` which is not defined, or trying to access the local object `a1` from `init()` in `main()`, which is out of its scope. Also, `X1.5=1;` is not valid C++ syntax; it looks like an attempt to assign a value to `x` or `a1.x`, but it's incorrectly written. A local object created inside a function is destroyed when that function finishes, making it inaccessible elsewhere.

Corrected/Intended usage might be:
`class A`
`{`
`public:`
`float x;`
`void init( )`
`{`
`x=1.5;`
`}`
`};`
`int main( )`
`{`
`A a1; // Object a1 is declared in main's scope`
`a1.init( ); // Call init on a1`
`}`
In simple words: The problem is that an object called `a1` is created inside a small part of the code (`init()`) but then another part of the code (`main()`) tries to use it, which is not allowed because `a1` only exists inside `init()`. Also, `X1.5=1;` is not a valid way to write code.

🎯 Exam Tip: Always pay attention to variable scope. Objects declared inside a function are local to that function and cannot be accessed directly from `main()` or other functions. Also, ensure correct syntax for assignments.

Activity – 4

 

Question. What is the size of the objects s1, s2?
`class sum`
`{`
`int n1,n2;`
`public:`
`void add( )`
`{`
`int n3=10;n1=n2=10;`
`}`
`}` s1,s2;`
Answer: The size of objects `s1` and `s2` will depend on the compiler and system architecture, specifically the size of an `int`.
Typically, in Dev C++ (a common compiler environment), an `int` is 4 bytes. Since `class sum` has two `int` data members (`n1` and `n2`), the total size of each object (`s1` or `s2`) would be `2 * 4 bytes = 8 bytes`.
In older compilers like Turbo C++, where an `int` might be 2 bytes, the size of each object would be `2 * 2 bytes = 4 bytes`. This variation highlights how compiler settings affect memory use. The member function `add()` and its local variable `n3` do not add to the object's size; they are part of the class's code segment, not each object's data.

Program to test the memory requirement:
`class sum`
`{`
`int n1,n2;`
`public:`
`void add( )`
`{`
`int n3-10;`
`n1=n2=10;`
`}`
`}` s1,s2;`
`using namespace std;`
`#include`
`int main( )`
`{`
`cout<`}`

Output:
`8 8`
In simple words: Each object, `s1` and `s2`, will be 8 bytes large in Dev C++ because it has two `int` variables, and each `int` takes 4 bytes. In older compilers like Turbo C++, it might be 4 bytes if an `int` is 2 bytes.

🎯 Exam Tip: Remember that `sizeof()` calculates only the memory taken by data members, not member functions or static members. The size of an `int` can vary (2 or 4 bytes) depending on the compiler and system architecture.

Activity – 5

 

Question. i) Write member function called display with no return. class objects. ii) Try the output of the above coding with the necessary modifications.
`PROGRAM`
`#include`
`using namespace std;`
`class compute`
`{`
`int n1, n2;`
`public :`
`void init (int a, int b)`
`{`
`n1 = a;`
`n2 = b;`
`}`
`int n;`
`int add ( )`
`{`
`return (n1+n2);`
`}`
`int prd ( )`
`{`
`return (n1*n2);`
`}`
`};`
`compute c1, c2;`
`void display(compute &obj1,compute &obj2)`
`{`
`c1.init(12,15);`
`c2.init(8,4);`
`// obj1.n = obj1.add( ); // Should be obj1.n if compute had a 'n' member and obj1 was a parameter.`
`// obj2.n = obj2.add( );`
`// These assignments are problematic because obj1 and obj2 are parameters, not global.`
`// The global c1, c2 are initialized in display, but `n` members are not accessed.`

`// Corrected logic for illustration based on original intent:`
`obj1.n = obj1.add(); // Assuming 'n' is a public member of compute`
`obj2.n = obj2.add(); // These obj1 and obj2 are passed in as references, so their 'n' would be set. But the original code doesn't define 'n' in compute.`

`// The code has issues in how 'n' is used and how objects are passed/accessed. Let's assume 'n' is meant to be a public member of compute and the global objects are being operated on.`

`c1.n = c1.add(); // Calculate sum for c1`
`c2.n = c2.add(); // Calculate sum for c2`

`cout<<"\\n Sum of object-1 "<`cout<<"\\n Sum of object-2 "<`cout<<"\\n Sum of the two objects are"<
`c1.init(5,4);`
`c2.init(2,5);`

`c1.n = c1.prd(); // Calculate product for c1`
`c2.n = c2.prd(); // Calculate product for c2`

`cout<<"\\n Product of object-1 "<`cout<<"\\n Product of object-2 "<`cout<<"\\n Product of the two objects are "<`}`
`int main( )`
`{`
`display(c1,c2);`
`return 0;`
`}`
Answer:
(i) The `compute` class needs a member function named `display` that does not return any value. This function will be `void display() { ... }`.

(ii) The provided code has some logical inconsistencies, particularly with the variable `n` and how `obj1.n` and `obj2.n` are used in the `display` function, which accepts `obj1` and `obj2` as references. The `compute` class definition has `int n;` but it's not clear how it's intended to be used with `add()` or `prd()`. Assuming `n` is meant to store the result of `add()` or `prd()`.

Here’s a corrected version of the code and its output, based on the likely intent to use the `add` and `prd` functions and then display results, storing them in the `n` member.

**Modified PROGRAM:**

#include 
using namespace std;

class compute {
    int n1, n2;
public:
    int n; // Assuming 'n' is meant to be public to store results for display
    void init(int a, int b) {
        n1 = a;
        n2 = b;
    }
    int add() {
        return (n1 + n2);
    }
    int prd() {
        return (n1 * n2);
    }
    void display_results(const char* label) { // New function for displaying specific object results
        cout << "\n" << label << " Sum: " << n1 + n2;
        cout << "\n" << label << " Product: " << n1 * n2;
    }
};

// Global objects declared outside main
compute global_c1, global_c2; 

void run_calculations_and_display() {
    global_c1.init(12, 15);
    global_c2.init(8, 4);

    global_c1.n = global_c1.add(); // Sum for global_c1
    global_c2.n = global_c2.add(); // Sum for global_c2

    cout << "\n Sum of global object-1: " << global_c1.n;
    cout << "\n Sum of global object-2: " << global_c2.n;
    cout << "\n Sum of the two global objects total: " << global_c1.n + global_c2.n;

    global_c1.init(5, 4); // Re-initialize for product calculation
    global_c2.init(2, 5);

    global_c1.n = global_c1.prd(); // Product for global_c1
    global_c2.n = global_c2.prd(); // Product for global_c2

    cout << "\n Product of global object-1: " << global_c1.n;
    cout << "\n Product of global object-2: " << global_c2.n;
    cout << "\n Product of the two global objects total: " << global_c1.n * global_c2.n;
}

int main() {
    run_calculations_and_display();
    return 0;
}
**Output from Modified Program:** ` Sum of global object-1: 27` ` Sum of global object-2: 12` ` Sum of the two global objects total: 39` ` Product of global object-1: 20` ` Product of global object-2: 10` ` Product of the two global objects total: 200`
In simple words: The code calculates sums and products for two `compute` objects. First, it adds numbers (12+15=27, 8+4=12) and shows their total (39). Then, it multiplies new numbers (5*4=20, 2*5=10) and shows their total product (200). The variable `n` within each object holds these calculated results for display.

🎯 Exam Tip: When given a program with errors or ambiguities, clarify the intended logic. Ensure that all variables are correctly declared and that functions are called with the appropriate objects and arguments, especially when dealing with global versus local objects or passing by reference.

Activity – 6

 

Question. In the above program justify your reason for no output.
`#include`
`using namespace std;`
`class Sample`
`{`
`int i,j;`
`public :`
`int k;`
`Sample( )`
`{`
`i=j=k=0;//constructor defined inside the class`
`}`
`};`
`int main( )`
`{`
`Sample s1;`
`return 0;`
`}`
Answer: The program produces no visible output because the constructor, although executed, does not contain any statement to print information to the console. The constructor `Sample()` initializes the data members `i`, `j`, and `k` to 0 when `s1` is created in `main()`. However, there is no `cout` statement within the constructor or any other part of the `main()` function or `Sample` class methods that would display this initialization or any other message. Therefore, when the program runs, it successfully creates and initializes an object, but it simply finishes without printing anything to the screen. To see output, a `cout` statement would need to be added, for instance, inside the constructor or a `display()` method called from `main()`.
In simple words: The program makes a `Sample` object and sets its numbers to zero, but it doesn't print anything because there's no command like `cout` to show what's happening on the screen.

🎯 Exam Tip: Always include `cout` statements if you expect output from a program, even when constructors or other functions are being executed internally. Initialization happens silently unless explicitly printed.

Hands-On Practice

 

Question 1. Define a class employee with the following specification.
Answer:

#include<iostream>
#include<iomanip>
using namespace std;

class Employee
{
private :
    int empno;
    char ename[20];
    float basic,hra,da,netpay;

    float calculate( )
    {
        return (basic+hra+da);
    }

public:
    void havedata( )
    {
        cout<<setw(35)<<"Enter Employee number :";
        cin>>empno;
        cout<<setw(35)<<"Enter Employee name :";
        cin>>ename;
        cout<<setw(35)<<"Enter Basic pay :";
        cin>>basic;
        cout<<setw(35)<<"Enter House Rent Allowance (HRA):";
        cin>>hra;
        cout<<setw(35)<<"Enter Dearness Allowance (DA):";
        cin>>da;
        netpay = calculate( );
    }

    void dispdata( )
    {
        cout<<"\nEMPLOYEE DETAILS\n\n";
        cout<<setw(35)<<"Employee number :"<<empno<<endl;
        cout<<setw(35)<<"Employee name :"<<ename<<endl;
        cout<<setw(35)<<"Basic pay :"<<basic<<endl;
        cout<<setw(35)<<"House Rent Allowance (HRA) :"<<hra<<endl;
        cout<<setw(35)<<"Dearness Allowance (DA) :"<<da<<endl;
        cout<<setw(35)<<"Netpay :"<<netpay<<endl;
    }
};

int main( )
{
    Employee e;
    e.havedata( );
    e.dispdata( );
    return 0; // Added return 0 for proper main function exit.
}
In simple words: This C++ program defines an `Employee` class. It stores an employee's number, name, basic pay, HRA, DA, and calculated net pay. The program takes employee details as input and then displays them, including the calculated total pay.

🎯 Exam Tip: When defining a class, ensure correct access specifiers (private, public) for members and use appropriate data types. Always include `return 0;` at the end of `main()` for good practice.

 

Question 2. Define a class MATH with the following specifications.
Answer:

#include<iostream>
#include<iomanip>
using namespace std;

class MATH
{
private:
    float num1,num2,result;

    void init( ) // Changed init to be a member function
    {
        num1=0;
        num2=0;
        result=0;
    }

protected:
    void add( )
    {
        result = num1+num2;
    }

    void diff( )
    {
        result = num1 - num2;
    }

public:
    void getdata( )
    {
        cout<<"\nEnter two numbers ";
        cin>>num1>>num2;
    }

    void menu( )
    {
        int choice;
        cout<<"\n1. Add ...";
        cout<<"\n2. Subtract .......";
        cout<<"\nEnter your choice :"; cin>>choice;

        switch(choice)
        {
            case 1:
                init(); // Call init to reset values
                getdata( );
                add( );
                cout<<"\nAdded value is "<<result;
                break;
            case 2:
                init(); // Call init to reset values
                getdata( );
                diff( );
                cout<<"\nSubtracted value is "<<result;
                break;
            default:
                cout<<"\nEnd";
        }
    }
};

int main( )
{
    MATH m;
    m.menu( );
    return 0; // Added return 0 for proper main function exit.
}
In simple words: This C++ program creates a `MATH` class to perform basic addition and subtraction. It takes two numbers from the user, then lets them choose between adding or subtracting them, and displays the result. It uses private, protected, and public parts to organize the code.

🎯 Exam Tip: Ensure that protected members are only accessed by member functions or derived classes. Use a `switch` statement for menu-driven programs to handle different user choices efficiently.

 

Question 3. Create a class called Item with the following specifications.
Answer:

#include<iostream>
#include<iomanip>
using namespace std;

class Item
{
private:
    int code,quantity;
    float price;
    void getdata( )
    {
        cout<<"\nEnter product code "; cin>>code;
        cout<<"\nEnter quantity "; cin>>quantity;
        cout<<"\nEnter price "; cin>> price;
    }

public:
    float tax;
    void display( )
    {
        getdata( );
        if(quantity>100)
            tax = 2500;
        else
            tax = 1000;

        cout<<endl<<setw(25)<< "Product code : "<<code<<endl<<endl;
        cout<<setw(25)<<"Quantity : " <<quantity<<endl<<endl;
        cout<<setw(25)<<"Unit price :" <<price<<endl<<endl;
        cout<<setw(25)<<"Total Amount: " <<setw(25)<<"Net Bill amount : "
              <<quantity* price+tax<<endl<<endl;
    }
};

int main( )
{
    Item i;
    i.display( );
    return 0; // Added return 0 for proper main function exit.
}
In simple words: This C++ program defines an `Item` class to store details like product code, quantity, and price. It calculates a tax based on quantity (2500 if over 100, else 1000) and then displays all the item details along with the final bill amount. The program helps manage product information and costs.

🎯 Exam Tip: Remember to use `setw()` for formatted output to align columns nicely, and ensure the logic for conditional tax calculation is correct. Private member functions like `getdata()` are often called by public member functions.

 

Question 4. Write the definition of a class FRAME in C++ with the following description.
Answer:

#include<iostream>
#include<iomanip>
using namespace std;

class FRAME
{
private:
    int FrameId;
    float Height, Width, Amount;

    void SetAmount()
    {
        Amount = 10 * Height * Width;
    }

public:
    void Getdetails( )
    {
        cout<<"\nEnter Frame Id : "; cin>> FrameId;
        cout<<"\nEnter Frame Height: "; cin>> Height;
        cout<<"\nEnter Frame Width : "; cin>>Width;
        SetAmount( ); // Calls private member function
    }

    void ShowDetails( )
    {
        cout<<endl<<setw(25)<<"Frame Id :" <<FrameId<<endl<<endl;
        cout<<setw(25)<<"Frame Height:" <<Height<<endl<<endl;
        cout<<setw(25)<<"Frame Width :"<<Width<<endl<<endl;
        cout<<setw(25)<<"Total Amount:"
              <<Amount<<endl<<endl;
    }
};

int main( )
{
    FRAME F;
    F.Getdetails( );
    F.ShowDetails( );
    return 0; // Added return 0 for proper main function exit.
}
In simple words: This C++ program defines a `FRAME` class to store frame details like ID, height, and width. It calculates a total amount based on these dimensions (10 * Height * Width). The program lets users input frame details and then displays them, including the calculated amount. It shows how to use a private function for calculations.

🎯 Exam Tip: Pay attention to how a private member function (`SetAmount()`) is called internally by a public member function (`Getdetails()`) to maintain data encapsulation.

 

Question 5. Define a class RESORT in C++ with the following description:
Answer:

#include<iostream>
using namespace std;

class RESORT
{
private:
    int Rno,Days,Charges;
    char Rname[20];

    int compute( )
    {
        if (Days * Charges > 5000)
            return (Days * Charges * 1.02);
        else
            return(Days * Charges);
    }

public:
    void getinfo( )
    {
        cout<<"\nEnter customer name :";
        cin>>Rname;
        cout<<"\nEnter charges per day :";
        cin>>Charges;
        cout<<"\nEnter Number of days :";
        cin>>Days;
        cout<<"\nEnter Room Number :";
        cin>>Rno;
    }

    void dispinfo( )
    {
        cout<<"\nRoom Number : "<<Rno;
        cout<<"\nCustomer name : "<<Rname;
        cout<<"\nCharges per day : "<<Charges;
        cout<<"\nNumber of days : "<<Days;
        cout<<"\nTotal Amount : "<<compute( ); // Call private compute function
    }
};

int main( )
{
    RESORT Obj;
    Obj.getinfo( );
    Obj.dispinfo( );
    return 0; // Added return 0 for proper main function exit.
}
In simple words: This C++ program defines a `RESORT` class to manage customer bookings. It stores room number, customer name, daily charges, and number of days. It calculates the total amount, adding a small extra charge if the total is over 5000. Users can enter booking details and then see a summary including the final bill.

🎯 Exam Tip: When a calculation depends on multiple internal class variables, use a private member function (like `compute()`) to perform that calculation and call it from a public function that displays the results.

 

Question 7. Create a BankAccount class with the following specifications
Answer:

#include<iostream>
#include<iomanip>
using namespace std;

struct pno
{
    int pin;
    float balance;
};

class BankAccount
{
public:
    pno pno_obj[10];

    void deposit(int pn,float amt)
    {
        for(int i=0;i<10;i++)
            if(pno_obj[i].pin == pn)
            {
                pno_obj[i].balance = pno_obj[i].balance + amt;
                cout<<"\nTransaction successful!";
                cout<<"\nBalance amount in your account is "<< pno_obj[i].balance;
                break;
            }
    }

    void withdraw(int type,int pn,float amt) // Renamed 'self' to 'type' as 'self' is a keyword in some languages.
    {
        for(int i=0;i<10;i++)
        {
            if(pno_obj[i].pin == pn)
            {
                // Assuming 'type' is not used, as per common C++ OOP structure,
                // and focusing on the core withdrawal logic.
                if (pno_obj[i].balance > 1000 && amt < pno_obj[i].balance)
                {
                    pno_obj[i].balance = pno_obj[i].balance - amt;
                    cout<<"\nTransaction successful";
                    cout<<"\nBalance amount in your account is "<< pno_obj[i].balance;
                    break;
                } else {
                    cout << "\nWithdrawal failed: Insufficient balance or amount too large.";
                    break;
                }
            }
        }
    }
};

int main( )
{
    int pin_no, tamt;
    BankAccount b;

    // Initialization of objects with pin and balance amount as 0
    for(int i=0;i<10;i++)
    {
        b.pno_obj[i].pin=i+1; // Pins from 1 to 10
        b.pno_obj[i].balance=0; // Initial balance is 0
    }

    int choice;
    while(choice !=3)
    {
        cout <<"\n1. Deposit";
        cout <<"\n2.Withdrawal";
        cout <<"\n3.Exit";
        cout <<"\nEnter your choice "; cin >>choice;

        switch(choice)
        {
            case 1:
                cout<<"\nEnter PIN: ";
                cin>>pin_no;
                cout<<"\nEnter Deposit amount: ";
                cin>>tamt;
                b.deposit(pin_no,tamt);
                break;
            case 2:
                cout<<"\nEnter PIN: ";
                cin>>pin_no;
                cout<<"\nEnter Withdrawal amount: ";
                cin>>tamt;
                // cout<<"\nEnter 1 for Self 2 for Others :"; // This line seems unused in withdrawal logic
                int type; // declared to match function prototype, but not used in logic
                // cin>>type;
                b.withdraw(type,pin_no,tamt);
                break;
            case 3:
                cout<<"\nTransaction completed";
                break;
            default:
                cout<<"\nInvalid choice. Please try again.";
        }
    }
    return 0; // Added return 0 for proper main function exit.
}
In simple words: This C++ program simulates a simple bank account system. It allows users to deposit and withdraw money using a PIN. The program stores up to 10 accounts, each with a unique PIN and balance. It checks if the PIN matches and if enough money is available for withdrawal before processing transactions.

🎯 Exam Tip: For array-of-structs or array-of-objects scenarios, ensure proper loop iteration (e.g., `for` loop) and correct indexing to access individual elements. Always handle conditions like insufficient balance for secure transactions.

 

Question 8. Define a class Hotel in C++ with the following description:
Answer: A class named `Hotel` is defined to manage hotel room data. It includes private members for room number, number of days stayed, charges per day, and customer name, along with a function to calculate the total amount. It also has public members for initializing the class, getting customer information, and displaying all details.
The `Calculate()` function determines the total amount based on days and charges. If the `Days * Charges` product is more than 12000, it returns `1.2 * Days * Charges`; otherwise, it returns `Days * Charges`. The `Hotel()` constructor sets default values to zero or empty strings.
The `Getinfo()` function asks the user for the customer's name, daily charges, number of days, and room number. The `Showinfo()` function then displays all these details along with the calculated total amount.

PROGRAM
using namespace std;
#include<iostream>
#include<string.h>
class Hotel
private:
int Rno,Days,Charges;
char Name[20];
1 int Calculate( )
{
if (Days * Charges >12000)
return (Days * Charges * 1.02);
else
return(Days * Charges);
}
public:
Hotel( )
{
Rno=0;
Days=0;
Charges=0;
strcpy(Name,””);
}
void Getinfo( )
{
cout<<β€œnEnter customer name :”;
cin>>Name;
cout<<β€œnEnter charges per day : β€œ;
cin>>Charges;
cout<<β€œ\nEnter Number of days :”;
cin>>Days;
cout<<β€œ\nEnter Room Number :”;
cin>>Rno;
}
void Showinfo( )
{
cout<<β€œ\nRoom Number: β€œ<<Rno;
cout<<β€œ\nCustomer name : β€œ<<Name;
cout<<β€œ\nCharges per day : β€œ<<Charges;
cout<<β€œ\nNumber of days : β€œ<<Days;
cout<<β€œ\nTota! Amount: β€œ<<Calculate( );
}
};
int main( )
{
Hotel obj;
obj.Getinfo( );
obj.Showinfo( )
}

In simple words: This program defines a `Hotel` class to manage customer bookings. It stores room details, customer name, and charges. It calculates the total bill, applying an extra charge if the bill is over 12000. It also has functions to get input from the user and display all the booking details.

🎯 Exam Tip: When defining a class, ensure all private, protected, and public members are clearly listed before providing the code implementation. This makes your design clear.

 

Question 9. Define a class Exam in C++ with the following description:
Answer: A class called `Exam` is defined to manage student examination data. It contains private members for roll number, marks, and candidate name. The class has a constructor to initialize these values and a destructor to display a message when an object is destroyed. A `Display()` function shows all student details. If the marks are above 60, it displays the roll number, name, and marks. If marks are 60 or less, it displays "Result Withheld".

PROGRAM
using namespace std;
#include<iostream>
#include<string.h>
class Exam
{
private:
int Rollno,Mark;
char Cname[25];
public:
Exam(int r,char n[25],int m)
{
Rollno = r;
Mark = m;
strcpy(Cname,n);
}
~Exam( )
{
cout<<β€œ\n\nResult will be intimated shortly”;
}
void Display( )
{
if (Mark>60)
{
cout<<β€œ\n\nRoll Number : β€œ<<Rollno;
cout<<β€œ\nCandidate name : β€œ<<Cname;
cout<<β€œ\nMark :”<<Mark;
}
else
{
cout<<β€œ\n\nRoll Number : β€œ<<Rollno;
cout<<β€œ\nCandidate name : β€œ<<Cname;
cout<<β€œ\nResult Withheld”;
}
}
};
int main( )
{
Exam obj 1(1011,”SURYA”,78),obj2( 1012,”JOHN”,44);
objl.Display( );
obj2. Display( );
}

In simple words: This code creates a class to handle exam results. It stores student roll number, name, and marks. It can show the details of students who scored over 60 marks, but for others, it will just say "Result Withheld". A message is shown when exam results are finalized.

🎯 Exam Tip: Remember that destructors are called automatically when an object goes out of scope, making them useful for cleanup messages or resource release.

 

Question 10. Define a class Student in C++ with the following specification:
Answer: A class named `Student` is defined to manage student academic records. It has private members for registration number, candidate name, aggregate marks, and grade. A `setGrade()` function calculates the grade based on marks. Public members include a constructor to assign default values, a copy constructor, a function `Getdata()` to input student information, a function `dispResult()` to display all details, and a destructor to show an "END" message.
Aggregate Marks -Grade
>=90 – A
Less than 90 and >=75 – B
Less than 75 and >=50 – C
Less than 50 – D

PROGRAM
using namespace std;
#include<iostream>
#include<string.h>
#include<iomanip>
class Student
{
private:
long Rno;
char Cname[25],Grade;
float Agg_marks;
void Setgrade()
{
if (Ag g_ma rks >=90)
Grade = β€˜A’;
else if(Agg_marks>=75)
Grade = β€˜B’;
else if(Agg_marks>=50)
Grade = β€˜C’;.
else
Grade = β€˜D’;
}
public:
Student( )
{
Rno = 0;
Agg_marks = 0;
strcpy(Cname,””);
Grade=”;
}
Student(Student &s) .
{
Rno = s.Rno;
Agg_marks = s.Agg_marks;
strcpy(Cname,s.Cname);
Grade=s.Grade;
}
~Student( )
{
cout<<β€œ\nEND”; >
}
void Getdata( )
{
cout<<β€œ\nEnter Register Number”;
cin>>Rno;
cout<<β€œ\nEnter Candidate Name
cin>>Cname;
cout<<β€œ\nEnter Aggrigate Mark”;
cin>>Agg_marks;
Setgrade( );
}
void dispResult( )
{
cout<<setw(30)<<β€œCandidate Register Number
β€œ<<Rno<<endl<<endl;
cout< <setw(30)<< β€œCandidate Name : β€œ<<Cname<<endk<endI;
cout<<setw(30)<<β€œAggrigate Mark : β€œ<<Agg_marksΒ«endk<endl;
cout<<setw(30)<<β€œGrade :
β€œ<<Grade<<endk<endl;
}
};
int main( )
{
Student s1;
s1.Getdata( );
Student s2(s1);
COut<<β€œ\nFIRST CANDIDATE DETAIL \n\n”;
s1.dispResult( );
cout<<β€œ\nSECOND CANDIDATE DETAIL \n\n”;
s2.dispResult( );
}

In simple words: This program defines a `Student` class to keep track of student details like roll number, name, and marks. It also calculates a grade based on the marks. There are functions to put in new student details and show them, including creating a copy of a student's record. A message is shown at the end of the program.

🎯 Exam Tip: When using `strcpy()`, ensure the destination character array is large enough to hold the source string to prevent buffer overflow issues.

Choose The Correct Answer (1 Mark)

 

Question 1. The most important feature of C++ is ………………..
(a) object
(b) class
(c) public
(d) All of the options
Answer: (d) All of the options
In simple words: Objects, classes, and public access are all key parts of C++ programming. They work together to build strong, organized code.

🎯 Exam Tip: Remember that C++ is an object-oriented programming language, where classes and objects are fundamental building blocks, and access specifiers like 'public' control visibility.

TN Board Solutions Class 11 Computer Science Chapter 14 Classes and Objects

Students can now access the TN Board Solutions for Chapter 14 Classes and Objects prepared by teachers on our website. These solutions cover all questions in exercise in your Class 11 Computer Science textbook. Each answer is updated based on the current academic session as per the latest TN Board syllabus.

Detailed Explanations for Chapter 14 Classes and Objects

Our expert teachers have provided step-by-step explanations for all the difficult questions in the Class 11 Computer Science chapter. Along with the final answers, we have also explained the concept behind it to help you build stronger understanding of each topic. This will be really helpful for Class 11 students who want to understand both theoretical and practical questions. By studying these TN Board Questions and Answers your basic concepts will improve a lot.

Benefits of using Computer Science Class 11 Solved Papers

Using our Computer Science solutions regularly students will be able to improve their logical thinking and problem-solving speed. These Class 11 solutions are a guide for self-study and homework assistance. Along with the chapter-wise solutions, you should also refer to our Revision Notes and Sample Papers for Chapter 14 Classes and Objects to get a complete preparation experience.

FAQs

Where can I find the latest Samacheer Kalvi Class 11 Computer Science Solutions Chapter 14 Classes and Objects for the 2026-27 session?

The complete and updated Samacheer Kalvi Class 11 Computer Science Solutions Chapter 14 Classes and Objects is available for free on StudiesToday.com. These solutions for Class 11 Computer Science are as per latest TN Board curriculum.

Are the Computer Science TN Board solutions for Class 11 updated for the new 50% competency-based exam pattern?

Yes, our experts have revised the Samacheer Kalvi Class 11 Computer Science Solutions Chapter 14 Classes and Objects as per 2026 exam pattern. All textbook exercises have been solved and have added explanation about how the Computer Science concepts are applied in case-study and assertion-reasoning questions.

How do these Class 11 TN Board solutions help in scoring 90% plus marks?

Toppers recommend using TN Board language because TN Board marking schemes are strictly based on textbook definitions. Our Samacheer Kalvi Class 11 Computer Science Solutions Chapter 14 Classes and Objects will help students to get full marks in the theory paper.

Do you offer Samacheer Kalvi Class 11 Computer Science Solutions Chapter 14 Classes and Objects in multiple languages like Hindi and English?

Yes, we provide bilingual support for Class 11 Computer Science. You can access Samacheer Kalvi Class 11 Computer Science Solutions Chapter 14 Classes and Objects in both English and Hindi medium.

Is it possible to download the Computer Science TN Board solutions for Class 11 as a PDF?

Yes, you can download the entire Samacheer Kalvi Class 11 Computer Science Solutions Chapter 14 Classes and Objects in printable PDF format for offline study on any device.