Thursday, 19 February 2026

Classes and Objects in C++

1. What is a Class?
A class is a user-defined data type that contains:
  • Variables (data members)
  • Functions (member functions)
👉 It acts as a blueprint for creating objects.

Syntax:
class ClassName {
    access_specifier:
        data_members;
        member_functions;
};

2. What is an Object?
An object is an instance of a class.

✔ Class = Blueprint
✔ Object = Real-world entity created from class

3. Simple Example
#include <iostream>
using namespace std;
class Student {
public:
    int roll;
    string name;
    void display() {
        cout << "Roll: " << roll << endl;
        cout << "Name: " << name << endl;
    }
};
int main() {
    Student s1;   // Object creation
    s1.roll = 101;
    s1.name = "Rahul";
    s1.display();
    return 0;
}

4. Access Specifiers
Access specifiers define visibility of class members.

Specifier      Meaning
public             Accessible everywhere
private             Accessible only inside class
protected     Accessible in derived classes

5. Private Members Example
class Bank {
private:
    int balance;
public:
    void setBalance(int b) {
        balance = b;
    }
    void showBalance() {
        cout << "Balance: " << balance;
    }
};
✔ Data hiding achieved using private members.

6. Defining Member Functions Outside Class
#include <iostream>
using namespace std;
class Rectangle {
public:
    int length, width;
    int area();
};
int Rectangle::area() {
    return length * width;
}
int main() {
    Rectangle r;
    r.length = 5;
    r.width = 4;
    cout << "Area: " << r.area();
}

7. Multiple Objects
Student s1, s2;
s1.roll = 1;
s2.roll = 2;
Each object has its own copy of data members.

8. Class with Constructor
A constructor initializes objects automatically.

class Demo {
public:
    Demo() {
        cout << "Constructor called!";
    }
};
int main() {
    Demo d1;
}

9. Real-Life Example
class Car {
public:
    string brand;
    int speed;
    void start() {
        cout << brand << " is starting...";
    }
};
Object:
Car c1;
c1.brand = "Tesla";
c1.start();

10. Key Features of Classes

✔ Encapsulation
✔ Data hiding
✔ Code reusability
✔ Modular programming

📌 Difference: Structure vs Class
Feature         Structure        Class
Default access public                 private
OOP features Limited                 Full support
Use case                 Simple data         Complex OOP programs

Member Functions in C++
Member functions are functions defined inside a class that operate on the data members of that class.
1. Definition
  • A member function:
  • Belongs to a class
  • Can access all data members of the class
  • Can be defined inside or outside the class
2. Basic Syntax
class ClassName {
public:
    void functionName();   // Declaration
};

3. Member Function Defined Inside Class
#include <iostream>
using namespace std;
class Student {
public:
    int roll;
    string name;
    void display() {
        cout << "Roll: " << roll << endl;
        cout << "Name: " << name << endl;
    }
};
int main() {
    Student s1;
    s1.roll = 101;
    s1.name = "Rahul";
    s1.display();   // Calling member function
}
✔ Automatically inline when defined inside class.

4. Member Function Defined Outside Class
Step 1: Declaration in class
Step 2: Definition using scope resolution operator ::

#include <iostream>
using namespace std;
class Rectangle {
public:
    int length, width;
    int area();
};
int Rectangle::area() {
    return length * width;
}
int main() {
    Rectangle r;
    r.length = 5;
    r.width = 4;
    cout << "Area: " << r.area();
}
👉 ClassName::functionName

5. Accessing Data Members
Member functions can directly access:
  • Public members
  • Private members
  • Protected members
Example:
class Bank {
private:
    int balance;
public:
    void setBalance(int b) {
        balance = b;   // Access private member
    }
};

6. Types of Member Functions
1.Inline Member Function
Defined inside class → automatically inline.

class Test {
public:
    void show() {
        cout << "Inline function";
    }
};

2. Non-Inline Member Function
Defined outside class.

void Test::show() {
    cout << "Non-inline function";
}

3. Const Member Function
Does not modify object data.

class Demo {
public:
    int x;
    void show() const {
        cout << x;
    }
};

4. Static Member Function
Belongs to class, not objects.

class Counter {
public:
    static int count;
    static void showCount() {
        cout << count;
    }
};
✔ Can access only static members.

0 comments

Post a Comment