Classes and objects
Chapter 14 · Tamil Nadu State Board
- 9 min read
- 5-question quiz
Every key point below shows the sentence from your textbook it comes from. Book back answers are solved and checked against the book; answers we could not prove are left out. How we check content
In short
This lesson explains how C++ classes bind data with the functions that work on it, and how objects are created from classes. It covers access specifiers, member functions, object memory and ways to refer to class members. It also teaches how constructors initialize objects and how destructors act when object lifetimes end. These ideas help students design and understand C++ programs using classes and objects.
Written by AI from this lesson. How we check it
Key points
- A class brings related data and functions together and can define a user-defined data type.
From your textbook
“Class is a way to bind the data and its associated functions together.”
- Class members can have private, public or protected access; members are private by default.
From your textbook
“The default access specifier for members is private.”
- Data members describe class properties, while member functions carry out tasks.
From your textbook
“Data members are the data variables that represent the features or properties of a class.”
- An object is a class variable, and it is also known as an instance of that class.
From your textbook
“The class variables are called object. Objects are also called as instance of class.”
- Each object gets separate memory for its data members, while objects of a class share its member functions.
From your textbook
“Memory space required for the member variables are only allocated separately for each object”
- A constructor is called automatically when an object is created and initializes its data members.
From your textbook
“The main function of the constructor is”
- A destructor runs when an object goes out of scope and helps release resources used by it.
From your textbook
“The purpose of the destructor is to free the resources that the object may have acquired during its lifetime.”
Formulas in this chapter
- Outline member function definitionreturn_type class_name :: function_name (parameter list) { function definition }Explained here
- Calling a member functionObject_name . function_name(actual parameter);Explained here
Why C++ Uses Classes
A class binds data and the functions that work with that data. It helps represent real-world entities with both properties and operations. A class also creates a user-defined data type.
Four features of object-oriented programming supported by C++
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Definition
- Class
- A user-defined type that groups data members and member functions together.
Definition
- Object
- A variable of a class type; it is also called an instance of the class.
Class Members and Access Specifiers
Data members store a class's features or properties. Member functions perform tasks and are also called methods. Data members are also called attributes.
Access to class members
| Access specifier | Who can access the members? |
|---|---|
| public | Accessible from anywhere outside the class within the program. |
| private | Accessible only by the class's member functions, not from outside the class. |
| protected | Not accessible from outside the class, but accessible in child classes called derived or inherited classes. |
Remember: Class members are private by default. Structure members are public by default.
Access specifiers control access to class members and support data hiding. If every member is private, an object cannot access the class members directly.
A class definition begins with the keyword class and a class name. Its members are written inside curly brackets, and the definition ends with a semicolon.
Defining Class Member Functions
Two ways to define a member function
- Inside the class definition: the function behaves like an inline function.
- Outside the class definition: the function is an outline, or non-inline, member function.
For an inline function, the compiler places a copy of its code where the function is called at compile time. Declaring a function with a looping construct, switch, or goto statement as inline is not advisable.
return_type class_name :: function_name (parameter list) { function definition }
return_type is the function's data type; class_name is the class; function_name is the member function; parameter list gives its parameters; function definition contains its instructions. The scope resolution operator (::) connects the function definition to its class.
In the Box example, setWidth is defined outside the class using Box :: setWidth. It assigns the value of w to width. The inline member function printWidth displays the width.
Creating Global and Local Objects
A class specification describes a class, but you need an object to use it. For example, in student s;, s is an instance of the student class.
Where objects are declared
| Object type | Declaration and access |
|---|---|
| Global object | Declared outside all function bodies or after the class declaration. Any function in the program can use it. |
| Local object | Declared inside a function. It cannot be accessed from outside that function. |
Memory Allocation for Objects
Each object gets its own memory for its member variables, because different objects can store different values. Member functions are shared by all objects of the class, so each object does not get a separate copy of them. The member variables receive memory when an object is created.
Worked example
A product class has int code, int quantity, and float price. What is the size of each product object in the illustration?
Try it first, then show the solutionSolution
- The illustration shows 4 bytes for code, 4 bytes for quantity, and 4 bytes for price.
- Add the three member-variable sizes: 4 bytes + 4 bytes + 4 bytes.
Answer: Each object, p1 and p2, occupies 12 bytes.
Both objects can use the common code area containing assignData() and Print(). The object memory shown is for the member variables, not separate copies of these shared member functions.
Remember: Objects have separate memory for member variables, but share the class member functions.
Referencing Class Members
Definition
- Dot operator
- The dot operator (.) connects an object name to the name of a class member that is being accessed.
Object_name . function_name(actual parameter);
Object_name identifies the object, function_name identifies the member function, and actual parameter supplies its arguments.
For example, Stud . execute(); calls execute() using the object Stud. An object's data member is also accessed with the object name and dot operator, as in c1.n.
Worked example
In the compute example, what values are stored in c1.n and c2.n, and what is their total?
Try it first, then show the solutionSolution
- c1.add(12,15) returns 27, so c1.n becomes 27.
- c2.add(8,4) returns 12, so c2.n becomes 12.
- Add the two stored values: 27 + 12.
Answer: c1.n is 27, c2.n is 12, and their total is 39.
A class can also have an array of objects. For example, student s[10]; creates 10 objects of the student class.
Remember: Use object_name.member_name to access a class member.
Constructors and Object Initialization
Definition
- Constructor
- A constructor is a special class member function that initializes an object and has the same name as its class.
A class definition creates a user-defined data type, but an object must be created and initialized. This process is called instantiating the object. A constructor runs automatically when a class object is created or comes into scope.
Class objects cannot be initialized at declaration in the same way as arrays or structures in the example. Class members have access specifiers such as private, protected, or public. A constructor provides a way to initialize the class object.
Main functions of a constructor
- Allocate memory space to the object.
- Initialize the data members of the class object.
Ways to define a constructor
| Inside the class | Outside the class |
|---|---|
| The constructor is written within the class definition. | The constructor is written outside the class definition. |
A constructor returns nothing and is not associated with a data type. In the Sample example, Sample() sets i, j, and k to 0.
Remember: A constructor has the class name, returns nothing, and initializes objects automatically.
Types of Constructors
Three constructor types
| Type | What it does |
|---|---|
| Default constructor | Accepts no parameter. If a class has no explicit constructor, the compiler automatically generates a default constructor. |
| Parameterized constructor | Accepts arguments so objects can be created with different initial values. |
| Copy constructor | Uses a reference to an existing object of the same class to initialize another object. |
A default constructor can be written as Data ::Data(). A parameterized constructor can be written as Data :: Data(int,int). A copy constructor is usually of the form Data (Data&), where Data is the class name.
When a copy constructor can be called
- When an object is passed as a parameter to a member function, as in void Data::putdata(Data x);
- When a member function returns an object, as in Data getdata() { }.
- When an object initializes another object of its own class, as in Data d1, d2 (d1);.
In the constructor illustration, d1 uses the no-parameter constructor, d2(10,20) uses the parameterized constructor, and d3(d2) uses the copy constructor.
Remember: No arguments means default; arguments provide initial values; an existing object is used by a copy constructor.
Public Constructors and Destructors
A constructor and a destructor declared under public access can be used when objects are created and destroyed. A constructor is called when an object is created. A destructor is called when the object is destroyed.
Definition
- Constructor
- A special member function called automatically when an object is created.
Definition
- Destructor
- A special member function called automatically when an object is destroyed.
Worked example
In the TestMeOut class, what are Function 1 and Function 2, and when are they called?
Try it first, then show the solutionSolution
- Function 1 is ~TestMeOut(), which has the class name preceded by ~.
- Function 2 is TestMeOut(), which has the same name as the class and no return type.
- Function 2 is called when an object is created. Function 1 is called when that object is destroyed.
Answer: Function 1 is the destructor. Function 2 is the constructor.
Differences Between a Constructor and a Destructor
Constructor and destructor
| Constructor | Destructor |
|---|---|
| Called when an object is created. | Called when an object is destroyed. |
| Has the same name as the class. | Has the class name preceded by ~. |
| Initialises an object when it is created. | Is invoked when the object is destroyed. |
RESORT Class Requirements
Class members and their purpose
- Private data members: Rno stores the room number; Name stores the user name; Charges stores the per-day charge; Days stores the number of days.
- Compute() calculates Days * Charges. If the total exceeds 11000, it calculates the amount as 1.02 * Days * Charges.
- Public member GetInfo() reads the name, room number, charges and number of days.
- Public member DispInfo() displays the entered details and the total amount calculated using Compute().
Student Program Output
The object s is created with r = 14 and m = 70. Its constructor prints a message and stores these values. printdet() adds 30 to marks, then prints the details. The program finally prints Back to Main.
Questions students ask
Short, direct answers to what students search for in this chapter.
What is a class in C++?
A class is a user-defined type that groups data and the functions that work with it. Its data members describe features or properties, while its member functions perform tasks. An object is a variable created using that class type.
What are public, private, and protected members in C++?
Public members can be accessed from anywhere outside the class within the program. Private members can be accessed only by the class's member functions. Protected members are also unavailable from outside the class, but child classes can access them.
What is the difference between inline and outline member functions?
An inline member function is defined inside the class. The compiler places a copy of its code where the function is called at compile time. An outline member function is defined outside the class and uses the scope resolution operator (::) with the class name.
What is the difference between global and local objects in C++?
A global object is declared outside all function bodies or after the class declaration, so any function in the program can use it. A local object is declared inside a function and cannot be accessed from outside that function.
How is memory allocated for objects in C++?
Each object receives separate memory for its member variables, because different objects may hold different values. Objects of the same class share the member functions, so a separate copy of each member function is not allocated for every object.
How do you access a class member in C++?
Access a class member by writing the object name, a dot operator, and the member name. For example, c1.n accesses the member n of object c1, and Stud . execute(); calls execute() using object Stud.
Show 9 more questions
What is a constructor in C++?
A constructor is a special member function that initializes a class object. It has the same name as the class and returns nothing. It runs automatically when an instance of the class is created or comes into scope.
What are the types of constructors?
The three types are default constructors, parameterized constructors, and copy constructors. A default constructor accepts no parameter, a parameterized constructor accepts arguments for initial values, and a copy constructor uses a reference to an existing object of the same class.
What does a copy constructor do?
A copy constructor initializes an object using an existing object of the same class. It is usually written in the form Data (Data&), where Data is the class name. For example, in Data d1, d2 (d1);, d2 is initialized using d1.
What is the difference between implicit and explicit constructor calls?
An implicit call invokes the parameterized constructor automatically when an object is created, as in simple s1(10,20). An explicit call writes the constructor name to create and initialize the object, as in simple s1=simple(10,20). It creates a temporary object that is destroyed after its expression is used.
What is dynamic initialization of objects in C++?
Dynamic initialization means providing an object's initial values during runtime. For example, values can be read into variables a and b, then passed to a constructor with X x(a,b). The constructor stores those values in the object's data members.
What are the characteristics of a constructor in C++?
A constructor has the same name as its class and has no return type. It runs automatically when an object is created, can have parameters, and can be overloaded. Constructors cannot be inherited, though a derived class can call the base class constructor.
Why is a destructor needed in C++?
A destructor frees resources that an object may have acquired during its lifetime. It runs when the object's lifetime ends and destroys the object. Its name is the class name prefixed with ~, and it has no arguments or return type.
What is a destructor in C++ and when is it called?
A destructor is a special member function whose name is the class name preceded by ~. It is called when an object is destroyed. In the example, ~TestMeOut() is the destructor, and its body prints “Leaving the exam hall” when the object is destroyed.
What does Compute() do in the RESORT class?
Compute() calculates the total amount by multiplying Days by Charges. If that amount exceeds 11000, the amount is calculated as 1.02 * Days * Charges. The DispInfo() function displays the total amount calculated using Compute().
Book back answers
15 textbook exercises, solved and checked against the book. Questions we could not prove are left out.
SECTION – A Choose the correct answer
The variables declared inside the class are known as
- (A) data
- (B) inline
- (C) method
- (D) attributes
Try it, then show the answerAnswer
(D) attributes
Data members are also called attributes.
From the book: “data members are also called as attributes.”
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
Try it, then show the answerAnswer
(C) nesting of member function
The chapter calls a member function calling another member function of the same class nesting of member functions.
From the book: “When a member function is called by another member function of the same class , it is calledas nesting of member functions.”
The member function defined within the class behave like ........ functions
- (A) inline
- (B) Non inline
- (C) Outline
- (D) Data
Try it, then show the answerAnswer
(A) inline
A member function defined inside a class behaves like an inline function.
From the book: “When a member function is defined inside a class, it behaves like inline functions.”
Which of the following access specifier protects data from inadvertent modifications?
- (A) Private
- (B) Protected
- (C) Public
- (D) Global
Try it, then show the answerAnswer
(A) Private
Private members cannot be accessed from outside the class, protecting the data from direct external changes.
From the book: “A private member cannot be accessed from outside the class.”
Practice
What does a class bring together?
Exam questions
Practice questions with model answers. These are not from an official paper.
How does a class differ from an object?2 marks
A class defines a user-defined type and its members. An object is a variable or instance created using that class type.
State the purpose of the three class access specifiers.5 marks
Public members can be accessed from outside the class. Private members cannot be accessed from outside the class and can be used by class member functions. Protected members can also be accessed in derived classes.
What is a constructor, and what are its main functions?5 marks
A constructor is a special member function with the same name as the class, called automatically when an object is created. Its main functions are to allocate memory space to the object and initialize the class object's data members.
When is a destructor executed, and why is it used?2 marks
A destructor is executed when an object goes out of scope or its lifetime ends. It is used to free resources the object acquired during its lifetime and remove memory allocated when it was created.
Watch out
- Do not confuse a class, which defines a type, with an object, which is an instance of that type.
- Do not assume all class members are public: members are private by default.
Still stuck on this lesson?
Get an AI answer grounded in your actual textbook — with the exact page reference.
Ask AI about this topic →