codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

39 / 87 topics
38OOP Concepts Overview39Classes and Objects40Class Methods41Constructors & Constructor Overloading42Destructors43Access Modifiers / Specifiers44Encapsulation45Abstraction46Friend Functions and Friend Classes47Operator Overloading
Tutorials/C++ Programming/Classes and Objects
⚡C++ Programming

Classes and Objects

Updated 2026-05-12
30 min read

Classes and Objects

In object-oriented programming (OOP), classes are blueprints for creating objects. A class defines a set of properties (data members) and behaviors (member functions) that the created objects will have. Understanding classes and objects is fundamental to mastering OOP in C++.

Introduction

Classes encapsulate data and functions that operate on that data, providing a way to organize code logically. Objects are instances of classes, and they hold actual values for the data members defined by the class. This tutorial will guide you through defining classes, creating objects, understanding data members, member functions, and the special this pointer.

Defining a Class

A class is defined using the class keyword followed by the class name and a pair of curly braces {} containing the class members. Here’s a simple example:

ClassDefinition.cpp
1#include <iostream>
2using namespace std;
3
4// Define a class named Car
5class Car {
6public:
7 // Data members
8 string brand;
9 int year;
10
11 // Member function to display car details
12 void display() {
13 cout << "Brand: " << brand << ", Year: " << year << endl;
14 }
15};
16
17int main() {
18 // Create an object of the Car class
19 Car myCar;
20
21 // Assign values to data members
22 myCar.brand = "Toyota";
23 myCar.year = 2023;
24
25 // Call member function
26 myCar.display();
27
28 return 0;
29}
Output
Brand: Toyota, Year: 2023

Explanation

  • Class Definition: The class Car defines a blueprint for creating car objects. It includes two data members (brand and year) and one member function (display()).
  • Object Creation: In the main() function, an object named myCar is created from the Car class.
  • Accessing Members: Data members are accessed using the dot operator (.), e.g., myCar.brand = "Toyota";.
  • Calling Member Functions: Member functions are also called using the dot operator, e.g., myCar.display();.

Data Members

Data members are variables that hold data specific to an object. They can be of any data type and are defined within the class. Here’s a more detailed example:

DataMembers.cpp
1#include <iostream>
2using namespace std;
3
4class Student {
5public:
6 string name;
7 int age;
8 double gpa;
9
10 void display() {
11 cout << "Name: " << name << ", Age: " << age << ", GPA: " << gpa << endl;
12 }
13};
14
15int main() {
16 Student student1;
17 student1.name = "Alice";
18 student1.age = 20;
19 student1.gpa = 3.8;
20
21 student1.display();
22
23 return 0;
24}
Output
Name: Alice, Age: 20, GPA: 3.8

Explanation

  • Data Members: name, age, and gpa are data members of the Student class.
  • Initialization: Values are assigned to these members after the object is created.

Member Functions

Member functions are functions that belong to a class. They can access and modify the data members of an object. Here’s an example with member functions:

MemberFunctions.cpp
1#include <iostream>
2using namespace std;
3
4class Rectangle {
5public:
6 int width;
7 int height;
8
9 // Constructor
10 Rectangle(int w, int h) {
11 width = w;
12 height = h;
13 }
14
15 // Member function to calculate area
16 int area() {
17 return width * height;
18 }
19};
20
21int main() {
22 Rectangle rect(5, 3);
23 cout << "Area: " << rect.area() << endl;
24
25 return 0;
26}
Output
Area: 15

Explanation

  • Constructor: A special member function that initializes objects. It has the same name as the class and no return type.
  • Member Function: area() calculates and returns the area of the rectangle.

The this Pointer

The this pointer is a keyword in C++ that points to the current object being used. It is particularly useful when there are local variables or parameters with the same name as data members. Here’s an example:

ThisPointer.cpp
1#include <iostream>
2using namespace std;
3
4class Point {
5public:
6 int x, y;
7
8 // Constructor
9 Point(int x, int y) {
10 this->x = x; // Using 'this' to distinguish between member and parameter
11 this->y = y;
12 }
13
14 void display() {
15 cout << "Point (" << x << ", " << y << ")" << endl;
16 }
17};
18
19int main() {
20 Point p(10, 20);
21 p.display();
22
23 return 0;
24}
Output
Point (10, 20)

Explanation

  • this Pointer: Used to differentiate between the object’s data members (x, y) and the constructor parameters (x, y).

Practical Example

Let’s create a complete program that models a bank account with deposit and withdraw functionalities:

BankAccount.cpp
1#include <iostream>
2using namespace std;
3
4class BankAccount {
5public:
6 string owner;
7 double balance;
8
9 // Constructor
10 BankAccount(string o, double b) : owner(o), balance(b) {}
11
12 // Member function to deposit money
13 void deposit(double amount) {
14 if (amount > 0) {
15 balance += amount;
16 cout << "Deposited: $" << amount << endl;
17 } else {
18 cout << "Invalid deposit amount." << endl;
19 }
20 }
21
22 // Member function to withdraw money
23 bool withdraw(double amount) {
24 if (amount > 0 && amount <= balance) {
25 balance -= amount;
26 cout << "Withdrew: $" << amount << endl;
27 return true;
28 } else {
29 cout << "Insufficient funds or invalid withdrawal amount." << endl;
30 return false;
31 }
32 }
33
34 // Member function to display account details
35 void display() {
36 cout << "Owner: " << owner << ", Balance: $" << balance << endl;
37 }
38};
39
40int main() {
41 BankAccount acc("John Doe", 1000.0);
42 acc.display();
43
44 acc.deposit(500.0);
45 acc.withdraw(200.0);
46
47 acc.display();
48
49 return 0;
50}
Output
Owner: John Doe, Balance: $1000
Deposited: $500
Withdrew: $200
Owner: John Doe, Balance: $1300

Explanation

  • Class Definition: BankAccount class with data members owner and balance.
  • Constructor: Initializes the account with an owner and initial balance.
  • Member Functions: deposit(), withdraw(), and display() to manage and display account details.

Summary

ConceptDescription
ClassBlueprint for creating objects.
ObjectInstance of a class with actual values for data members.
Data MembersVariables that hold data specific to an object.
Member FunctionsFunctions that operate on the data members of an object.
this PointerPoints to the current object, useful for distinguishing between member variables and parameters.

What's Next?

In the next tutorial, we will explore more about class methods, including constructors, destructors, and operator overloading. Understanding these concepts will further enhance your ability to design and implement robust C++ programs using OOP principles.

Stay tuned!


PreviousOOP Concepts OverviewNext Class Methods

Recommended Gear

OOP Concepts OverviewClass Methods