📌 Class and Objects in C++

 


🔹 What are Classes and Objects?

  • A class is a user-defined blueprint that defines data members (variables) and member functions (methods).
  • An object is an instance of a class.

📌 Example 1: Class and Object in C++

🚀 Approach

  1. Define a class Car with attributes brand and model.
  2. Create an object and initialize values.

🔹 C++ Code

#include <iostream>
using namespace std;

// Define Class
class Car {
public:
    string brand;
    string model;

    // Method to display car details
    void showDetails() {
        cout << "Car Brand: " << brand << endl;
        cout << "Car Model: " << model << endl;
    }
};

int main() {
    Car myCar;  // Object creation
    myCar.brand = "Toyota";
    myCar.model = "Corolla";

    myCar.showDetails();
    return 0;
}

🔹 Output

Car Brand: Toyota
Car Model: Corolla

📌 Example 2: Find Box Volume using Objects

🚀 Approach

  1. Define a class Box with length, width, and height.
  2. Create an object, assign values, and calculate volume.

🔹 C++ Code

#include <iostream>
using namespace std;

// Define Class
class Box {
public:
    float length, width, height;

    // Function to calculate volume
    float volume() {
        return length * width * height;
    }
};

int main() {
    Box b1;  // Object creation
    b1.length = 5.0;
    b1.width = 4.0;
    b1.height = 3.0;

    cout << "Box Volume: " << b1.volume() << endl;
    return 0;
}

🔹 Output

Box Volume: 60

📌 Example 3: Object Creation in C++

🚀 Approach

  1. Define a class Person with name and age.
  2. Create an object, assign values, and display them.

🔹 C++ Code

#include <iostream>
using namespace std;

// Define Class
class Person {
public:
    string name;
    int age;

    void display() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

int main() {
    Person p1;  // Object creation
    p1.name = "John";
    p1.age = 25;

    p1.display();
    return 0;
}

🔹 Output

Name: John, Age: 25

📌 Example 4: Basic Example of Multiple Objects

🚀 Approach

  1. Define a class Student with attributes name and marks.
  2. Create multiple objects to store student details.

🔹 C++ Code

#include <iostream>
using namespace std;

// Define Class
class Student {
public:
    string name;
    float marks;

    void showDetails() {
        cout << "Student: " << name << ", Marks: " << marks << endl;
    }
};

int main() {
    Student s1, s2; // Creating multiple objects

    s1.name = "Alice";
    s1.marks = 85.5;

    s2.name = "Bob";
    s2.marks = 90.0;

    s1.showDetails();
    s2.showDetails();
    
    return 0;
}

🔹 Output

Student: Alice, Marks: 85.5
Student: Bob, Marks: 90.0

📌 Summary

Example Description
Class and Object Example Basic class and object usage.
Find Box Volume using Objects Computes volume using object attributes.
Object Creation Example Demonstrates object properties and method calls.
Basic Example of Multiple Objects Shows how to create multiple objects.

🚀 Need more examples? Let me know! 🚀

Post a Comment

0 Comments