Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Classes and Objects

Classes and Objects

In Object-Oriented Programming (OOP), classes and objects are fundamental concepts. To grasp them, let’s explore an analogy and then translate that into C++ code.

Real-Life Analogy: Designing a Car

Imagine you’re a car manufacturer. Before you start building cars, you need a blueprint that defines the features and specifications of the car. This blueprint isn’t a car itself, but it contains all the details necessary to create one. This is analogous to a class in OOP.

Once you have the blueprint, you can create actual cars based on it. Each car you manufacture is a unique instance but follows the same specifications laid out in the blueprint. These cars are analogous to objects.

C++ Class

A class is a blueprint or a template that defines the properties (attributes) and behaviors (methods) of objects. In our analogy, the class would define the characteristics of a car, such as the model, color, and engine type, as well as the actions the car can perform, like accelerating, braking, and honking.

Create a Class

To define a class in C++, use the class keyword followed by the class name. Inside the class, you can define its attributes (data members) and methods (member functions).

class ClassName {
public:
    // Public members (attributes and methods)

private:
    // Private members (attributes and methods)
};

C++ Object

An object is an instance of a class. Using the car analogy, an object would be a specific car with a particular model, color, and engine type. You can create multiple objects from the same class, each with its own unique set of properties.

Syntax
ClassName object_name;

For Example:

#include <iostream>
#include <string>
using namespace std;

// Define the Student class
class Student {
public:
    // Attributes
    string name;
    int rollNumber;
    float marks;

    // Method to display student details
    void displayDetails() {
        cout << "Name: " << name << endl;
        cout << "Roll Number: " << rollNumber << endl;
        cout << "Marks: " << marks << endl;
    }
};
int main() {
    // Create an object of the Student class
    Student student1;

    // Set attributes for student1
    student1.name = "Peter";
    student1.rollNumber = 23;
    student1.marks = 95.5;

    // Create another object of the Student class
    Student student2;

    // Set attributes for student2
    student2.name = "John";
    student2.rollNumber = 24;
    student2.marks = 88.0;

    // Display details of student1
    cout << "Student 1 Details:" << endl;
    student1.displayDetails();

    // Display details of student2
    cout << "Student 2 Details:" << endl;
    student2.displayDetails();

    return 0;
}

Output:

Student 1 Details:
Name: Peter
Roll Number: 23
Marks: 93.5
Student 2 Details:
Name: John
Roll Number: 24
Marks: 91

Explanation:
Step 1: Include Necessary Libraries

First, we include the necessary libraries to use standard input-output and string manipulation functionalities.

#include <iostream>  // For input and output
#include <string>    // For using string data type
using namespace std; // To avoid using std:: prefix repeatedly
Step 2: Define the Student Class

Next, we define a Student class, which serves as a blueprint for creating student objects. The class contains attributes (properties) and a method (function).

// Define the Student class
class Student {
public:
    // Attributes
    string name;      // To store the student's name
    int rollNumber;   // To store the student's roll number
    float marks;      // To store the student's marks

    // Method to display student details
    void displayDetails() {
        cout << "Name: " << name << endl;
        cout << "Roll Number: " << rollNumber << endl;
        cout << "Marks: " << marks << endl;
    }
};

Explanation

  • Attributes:
    • string name; — Stores the name of the student.
    • int rollNumber; — Stores the roll number of the student.
    • float marks; — Stores the marks of the student.
  • Method:
    • void displayDetails(); — This method displays the student’s details when called.
Step 3: Create and Initialize Objects in main()

In the main() function, we create objects of the Student class, set their attributes, and display their details using the method we defined.

int main() {
    // Create an object of the Student class
    Student student1;

    // Set attributes for student1
    student1.name = "Peter";
    student1.rollNumber = 23;
    student1.marks = 93.5;

    // Create another object of the Student class
    Student student2;

    // Set attributes for student2
    student2.name = "John";
    student2.rollNumber = 24;
    student2.marks = 91;

Explanation:

Creating Objects:

  • Student student1; — Creates an object student1 of the Student class.
  • Student student2; — Creates another object student2 of the Student class.

Setting Attributes:

  • student1.name = "Peter"; — Assigns “Peter” to the name attribute of student1.
  • student1.rollNumber = 23; — Assigns 23 to the rollNumber attribute of student1.
  • student1.marks = 93.5; — Assigns 93.5 to the marks attribute of student1.
  • The same steps are repeated for student2, with different values
Step 4: Display the Detail of Each Student

Finally, we call the displayDetails() method on each object to print their details to the console.

    // Display details of student1
    cout << "Student 1 Details:" << endl;
    student1.displayDetails();

    // Display details of student2
    cout << "Student 2 Details:" << endl;
    student2.displayDetails();

    return 0;
}

Explanation:

Calling Methods:

  • student1.displayDetails(); — Calls the displayDetails() method on student1, which prints the name, roll number, and marks of student1.
  • student2.displayDetails(); — Calls the displayDetails() method on student2, which prints the name, roll number, and marks of student2.

Output:

  • This will display the details of both students on the console.

Example 2:

#include <iostream>
#include <string>
using namespace std;

// Define the Book class
class Book {
public:
    // Attributes (Properties)
    string title;
    string author;
    int pages;

    // Method to display book details
    void displayDetails() {
        cout << "Title: " << title << endl;
        cout << "Author: " << author << endl;
        cout << "Pages: " << pages << endl;
    }
};
int main() {
    // Create an object of the Book class
    Book book1;

    // Set attributes for book1
    book1.title = "The Catcher in the Rye";
    book1.author = "J.D. Salinger";
    book1.pages = 277;

    // Create another object of the Book class
    Book book2;

    // Set attributes for book2
    book2.title = "To Kill a Mockingbird";
    book2.author = "Harper Lee";
    book2.pages = 281;

    // Display details of book1
    cout << "Book 1 Details:" << endl;
    book1.displayDetails();

    // Display details of book2
    cout << "Book 2 Details:" << endl;
    book2.displayDetails();

    return 0;
}

Output:

Book 1 Details:
Title: The Catcher in the Rye
Author: J.D. Salinger
Pages: 277
Book 2 Details:
Title: To Kill a Mockingbird
Author: Harper Lee
Pages: 281
Explanation:
Step 1: Include Necessary Libraries

First, we include the necessary libraries for input-output operations and string handling.

#include <iostream>  // For input and output operations
#include <string>    // For using the string data type
using namespace std; // To avoid using the std:: prefix before standard library functions
Step 2: Define the Book Class

Next, we define the Book class, which serves as a blueprint for creating book objects. The class contains attributes (properties) and a method (function).

// Define the Book class
class Book {
public:
    // Attributes (Properties)
    string title;   // To store the title of the book
    string author;  // To store the author of the book
    int pages;      // To store the number of pages in the book

    // Method to display book details
    void displayDetails() {
        cout << "Title: " << title << endl;
        cout << "Author: " << author << endl;
        cout << "Pages: " << pages << endl;
    }
};

Explanation:

  • Class Definition:
    • class Book { ... }; defines a class named Book.
  • Attributes:
    • string title; — Stores the title of the book.
    • string author; — Stores the author of the book.
    • int pages; — Stores the number of pages in the book.
  • Method:
    • void displayDetails(); — This method displays the book’s details.
Step 3: Create and Initialize Objects in main()

In the main() function, we create objects of the Book class, set their attributes, and display their details using the method we defined.

int main() {
    // Create an object of the Book class
    Book book1;

    // Set attributes for book1
    book1.title = "The Catcher in the Rye";
    book1.author = "J.D. Salinger";
    book1.pages = 277;

Explanation:

  • Creating an Object:
    • Book book1; creates an object named book1 of the Book class.
  • Setting Attributes:
    • book1.title = "The Catcher in the Rye"; sets the title attribute of book1.
    • book1.author = "J.D. Salinger"; sets the author attribute of book1.
    • book1.pages = 277; sets the pages attribute of book1.
Step 4: . Create Another Object and Initialize It

We can create another object of the Book class and assign it different attribute values.

    // Create another object of the Book class
    Book book2;

    // Set attributes for book2
    book2.title = "To Kill a Mockingbird";
    book2.author = "Harper Lee";
    book2.pages = 281;

Explanation:

  • Creating Another Object:
    • Book book2; creates another object named book2 of the Book class.
  • Setting Attributes:
    • book2.title = "To Kill a Mockingbird"; sets the title attribute of book2.
    • book2.author = "Harper Lee"; sets the author attribute of book2.
    • book2.pages = 281; sets the pages attribute of book2.
Step 5: Display the Details of Each Book

Finally, we use the displayDetails() method to print the details of each book.

    // Display details of book1
    cout << "Book 1 Details:" << endl;
    book1.displayDetails();

    // Display details of book2
    cout << "Book 2 Details:" << endl;
    book2.displayDetails();

    return 0;
}

Explanation:

  • Calling Methods:
    • book1.displayDetails(); calls the displayDetails() method on book1, which prints the title, author, and pages of book1.
    • book2.displayDetails(); calls the displayDetails() method on book2, which prints the title, author, and pages of book2.
  • Output:
    • This will display the details of both books on the console.

    Leave a Reply

    Your email address will not be published.

    Need Help?