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:
Studentis the class name.nameandageare data members.publicallows 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→ classs1→ 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:
- Data members
- Member functions
- Constructors
- Destructors
- Access specifiers
- 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:
nameis a data member.ageis a data member.marksis 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 Specifier | Same Class | Derived Class | Outside Class |
|---|---|---|---|
public | Yes | Yes | Yes |
private | Yes | No directly | No |
protected | Yes | Yes | No |
Example
class Student { public: string name; private: int age; protected: int rollNo; };
13. Class vs Object
| Class | Object |
|---|---|
| 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: Student | Example: 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
- Supports Object-Oriented Programming.
- Provides data organization.
- Supports encapsulation.
- Improves code reusability.
- Makes programs easier to maintain.
- Provides data hiding through access specifiers.
- Allows multiple objects to be created from one class.
- 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. publicmembers can be accessed from outside the class.privatemembers 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