Ugenz Academy/Tutorial

Classandobjects

8 min read1,428 words

Reading progress

Keep learning

Class and Object in C++

1. Introduction

Class and Object are fundamental concepts of Object-Oriented Programming (OOP) in C++.

  • A class is a blueprint or template for creating objects.
  • An object is an instance of a class.

Simple Example

Think of a class as a blueprint for a house.

  • Blueprint → Class
  • Actual house → Object

2. What is a Class?

A class is a user-defined data type that groups data members and member functions together.

Syntax

class ClassName { // data members // member functions };

Example

class Student { public: string name; int age; };

Here:

  • Student is the class name.
  • name and age are data members.
  • public allows them to be accessed outside the class.

3. What is an Object?

An object is a real instance of a class.

Syntax

ClassName objectName;

Example

Student s1;

Here:

  • Student → class
  • s1 → object

4. Simple Class and Object Example

#include <iostream> using namespace std; class Student { public: string name; int age; }; int main() { Student s1; s1.name = "Anu"; s1.age = 22; cout << "Name: " << s1.name << endl; cout << "Age: " << s1.age << endl; return 0; }

Output

Name: Anu Age: 22

5. Components of a Class

A class can contain:

  1. Data members
  2. Member functions
  3. Constructors
  4. Destructors
  5. Access specifiers
  6. Other classes and objects

Example

class Student { private: int rollNo; public: string name; void display() { cout << name; } };

6. Data Members

Variables declared inside a class are called data members.

Example

class Student { public: string name; int age; float marks; };

Here:

  • name is a data member.
  • age is a data member.
  • marks is a data member.

7. Member Functions

Functions declared inside a class are called member functions.

Example

#include <iostream> using namespace std; class Student { public: string name; void display() { cout << "Student Name: " << name; } }; int main() { Student s1; s1.name = "Anu"; s1.display(); return 0; }

Output

Student Name: Anu

8. Multiple Objects

We can create multiple objects from one class.

Example

#include <iostream> using namespace std; class Student { public: string name; int age; void display() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; int main() { Student s1, s2; s1.name = "Anu"; s1.age = 22; s2.name = "Ram"; s2.age = 21; s1.display(); cout << endl; s2.display(); return 0; }

Output

Name: Anu Age: 22 Name: Ram Age: 21

9. Accessing Class Members

The dot (.) operator is used to access public members of an object.

Syntax

objectName.memberName;

Example

Student s1; s1.name = "Anu"; s1.age = 22; cout << s1.name; cout << s1.age;

10. Class with Function Outside the Class

A member function can be defined outside the class using the scope resolution operator ::.

Example

#include <iostream> using namespace std; class Student { public: string name; void display(); }; void Student::display() { cout << "Name: " << name; } int main() { Student s1; s1.name = "Anu"; s1.display(); return 0; }

Output

Name: Anu

11. Class with Private Data

By default, members of a C++ class are private.

Private members cannot be directly accessed outside the class.

Example

#include <iostream> using namespace std; class Student { private: int age; public: void setAge(int a) { age = a; } void displayAge() { cout << "Age: " << age; } }; int main() { Student s1; s1.setAge(22); s1.displayAge(); return 0; }

Output

Age: 22

The following would produce an error:

s1.age = 22;

because age is private.

12. Access Specifiers

C++ provides three main access specifiers:

Access SpecifierSame ClassDerived ClassOutside Class
publicYesYesYes
privateYesNo directlyNo
protectedYesYesNo

Example

class Student { public: string name; private: int age; protected: int rollNo; };

13. Class vs Object

ClassObject
Class is a blueprint.Object is an instance of a class.
It defines properties and behavior.It represents a specific entity.
It does not represent a specific individual object.It represents a specific instance.
Memory for non-static data members is associated with objects.Object occupies memory for its data members.
Example: StudentExample: s1

Example

class Student { // Class }; Student s1; // Object Student s2; // Object

14. Real-Life Example

Consider a Car.

A car can have:

  • Brand
  • Model
  • Color
  • Speed

And it can perform actions such as:

  • Start
  • Stop
  • Accelerate

We can represent this using a class.

#include <iostream> using namespace std; class Car { public: string brand; string color; void start() { cout << "Car started." << endl; } void stop() { cout << "Car stopped." << endl; } }; int main() { Car car1; car1.brand = "Toyota"; car1.color = "White"; cout << "Brand: " << car1.brand << endl; cout << "Color: " << car1.color << endl; car1.start(); car1.stop(); return 0; }

Output

Brand: Toyota Color: White Car started. Car stopped.

15. Class and Object with User Input

#include <iostream> using namespace std; class Student { public: string name; int age; float marks; void input() { cout << "Enter name: "; cin >> name; cout << "Enter age: "; cin >> age; cout << "Enter marks: "; cin >> marks; } void display() { cout << "\nStudent Details" << endl; cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Marks: " << marks << endl; } }; int main() { Student s1; s1.input(); s1.display(); return 0; }

Example Input

Enter name: Anu Enter age: 22 Enter marks: 85.5

Output

Student Details Name: Anu Age: 22 Marks: 85.5

16. Advantages of Classes and Objects

  1. Supports Object-Oriented Programming.
  2. Provides data organization.
  3. Supports encapsulation.
  4. Improves code reusability.
  5. Makes programs easier to maintain.
  6. Provides data hiding through access specifiers.
  7. Allows multiple objects to be created from one class.
  8. Helps model real-world entities.

17. Example of Multiple Students

#include <iostream> using namespace std; class Student { public: string name; int marks; void display() { cout << name << " - " << marks << endl; } }; int main() { Student s1, s2, s3; s1.name = "Anu"; s1.marks = 85; s2.name = "Ram"; s2.marks = 78; s3.name = "Sita"; s3.marks = 92; s1.display(); s2.display(); s3.display(); return 0; }

Output

Anu - 85 Ram - 78 Sita - 92

18. Important Points

  • A class is a blueprint for objects.
  • An object is an instance of a class.
  • A class can contain data members and member functions.
  • The . operator is used to access public members through an object.
  • public members can be accessed from outside the class.
  • private members cannot be directly accessed from outside the class.
  • One class can have many objects.
  • Each object has its own non-static data members.
  • Classes are an important foundation of C++ OOP.

19. Basic Syntax to Remember

#include <iostream> using namespace std; class ClassName { public: // Data member int data; // Member function void display() { cout << data; } }; int main() { // Creating object ClassName obj; // Accessing members obj.data = 10; obj.display(); return 0; }

Output

10

20. Summary

Class → Blueprint or template.

Object → Instance of a class.

Data Member → Variable inside a class.

Member Function → Function inside a class.

Access Specifier → Controls access to class members.

The basic relationship is:

Class ↓ Creates ↓ Objects ↓ Objects contain ↓ Data + Functions

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy