Menu

What is Class and Object in OOP? Explained in Simple Words
📰

What is Class and Object in OOP? Explained in Simple Words

DEV Community: cpp·Aliza Kanwal·4 days ago
#FXry6wxI
#devtocpp#devto#dev#include#name
Reading 0:00
15s threshold

Aliza Kanwal

Class is a blueprint. Object is a real thing made from that blueprint.

Example: Agar Class "Car ka Naksha" hai, to Object us nakshay se bani hui asli Car hai.

Ek Class se hum jitne marzi Objects bana sakte hain, jaise neeche code me s1 aur s2 banaye hain.


cpp
#include <iostream>
using namespace std;

class Student {
public:
    string name;
    int rollNo;

    void introduce() {
        cout << "My name is " << name << endl;
        cout << "My Roll No is " << rollNo << endl;
    }
};

int main() {
    Student s1;
    s1.name = "Aliza";
    s1.rollNo = 1;
    s1.introduce();

    Student s2;
    s2.name = "Ayesha";
    s2.rollNo = 2;
    s2.introduce();

    return 0;
}

Enter fullscreen mode Exit fullscreen mode