CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Cyber Safety and Ethics · 7 min · free

Classes and Objects with Constructors and Destructors

7 min4 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

A C++ class holds private data and public functions; a constructor sets up a new object and a destructor cleans up when it dies.

Follows the syllabus of: NIOS Senior Secondary Computer Science (330)

On screen in this lesson

private versus public

SpecifierWho can use itUsually for
privateonly the classdata members
publicanyonemember functions
(no label)privateclass default

What is a constructor?

A special member function
Same name as the class
No return type, not even void
Runs automatically when an object is created
Used to give starting values

Types of constructors

TypeArgumentsExample
DefaultnoneAcc()
Parameterizedone or moreAcc(int b)
Copysame-class objectAcc(const Acc &a)

What is a destructor?

Same name as the class, with a tilde ~
No arguments and no return type
Only one destructor per class
Runs automatically when the object is destroyed

Object lifetime

Lifetime: from creation to destruction
A local object dies at its closing brace
Objects are destroyed in reverse order
Last created, first destroyed

Constructor vs destructor

PointConstructorDestructor
NameClassName()~ClassName()
Argumentsallowednot allowed
Overloadingallowednot possible
Runs whenobject createdobject destroyed

Quick answers

Which destructor runs first for objects a then b?

b, because objects are destroyed in reverse order.

Does a constructor have a return type?

No, not even void.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. When you open a new bank account, the bank sets it up before you use it. Who sets up a new object in C plus plus, and who cleans up after it? Today we learn to define a class, create objects, and write constructors and destructors.

A class is defined with the keyword class, then its name, then curly braces. Inside, we write data members, which are variables, and member functions, which are functions. Here roll and marks are data members, and show is a member function. The section labels private and public are called access specifiers. Never forget the semicolon after the closing brace.

Access specifiers decide who can use a member. A private member can be used only by the functions of the same class. A public member can be used from anywhere, including main. If we write no label, members of a class are private by default. So we usually keep data private and functions public, which is data hiding.

Defining a class uses no memory for data. Memory is given only when we create an object. We create an object just like a variable, by writing the class name and then the object name. Box a, b creates two objects of class Box. Each has its own len. The dot operator reaches a public member, so it prints five and eight.

Here, Acc is a bank account class, and bal, the balance, is private. The object s is short for savings. Writing s dot bal in main would give a compile error, because bal is private. So main calls the public function set, with five hundred. The public function get returns the balance, and it prints five hundred.

Setting values by calling set every time is easy to forget. A constructor solves this. A constructor is a special member function of a class. It has exactly the same name as the class. It has no return type, not even void. It runs by itself when an object is created. Its job is to initialise the object, which means giving its data members starting values.

A constructor that takes no arguments is called a default constructor. Here, Acc with empty brackets is the constructor, and it sets the balance to zero. In main, we only write Acc s. We never call the constructor ourselves, yet it runs. So get returns zero, and it prints zero.

A constructor can also take arguments, and then it is called a parameterized constructor. Here the constructor takes an integer b and stores it in bal. We pass the starting value in round brackets when we create the object. Acc s of one thousand opens the account with one thousand rupees. So it prints one thousand.

A class can have more than one constructor, if their argument lists are different. This is called constructor overloading. Here Acc has a default constructor and a parameterized one. For object x, we pass nothing, so the default constructor sets zero. For object y, we pass seven hundred, so the second constructor runs. It prints zero and seven hundred.

Exams ask for three types of constructors. A default constructor takes no arguments. A parameterized constructor takes one or more arguments. A copy constructor takes another object of the same class, and makes a new object as a copy of it. If you write no constructor at all, the compiler quietly provides a default one.

Now the clean up job. A destructor is a special member function that runs when an object is destroyed. Its name is the class name with a tilde in front, the wavy line symbol. It takes no arguments and has no return type. Because it takes no arguments, it cannot be overloaded, so a class has only one destructor. It runs by itself, and it is used to free memory or close files.

Let us watch both run. T is a small class, and t is its object. When t is created, the constructor prints Hi. Then main prints Work. When main ends, t goes out of scope, which means its block is finished. So the destructor runs and prints Bye. The output is Hi Work Bye.

The time between creating an object and destroying it is called its lifetime. A local object is created where it is declared. It is destroyed when control reaches the closing brace of its block. When several objects end together, they are destroyed in reverse order of creation. The last one created is the first one destroyed, like plates stacked in a canteen.

Pause and predict. Here P is a struct, which works like a class whose members are public. Object a stores A, then object b stores B. Main prints a dash. At the closing brace, which destructor runs first? The destructor of b runs first, because b was created last. So the output is dash, B, A.

Here is the comparison exams like. A constructor has the class name, and a destructor has the class name with a tilde. A constructor can take arguments, but a destructor cannot. So constructors can be overloaded, but a destructor cannot. A constructor runs when an object is created, and a destructor runs when it is destroyed.

Let us revise what we learned today. Keep data members private and member functions public. Create an object by writing the class name and then the object name. A constructor has the class name and no return type, and it runs automatically. Constructor overloading means many constructors with different arguments. A destructor has a tilde before the name, and it runs when the object is destroyed, in reverse order.

Courses that teach this

CourseUnit
NIOS Senior Secondary Computer Science (330)Module 3: Programming in C++
Programming All levels C++Object-Oriented Programming

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →