Metadata
- π Date :: 23-04-2025
- π·οΈ Tags :: cpp
Notes
Detailed and Elaborative Notes on Object-Oriented Programming (OOP) in C++
1. Introduction to Objects and Object-Oriented Programming (OOP)
In Object-Oriented Programming (OOP), the fundamental concept is to use objects that mimic real-world items. These objects are collections of two main components:
- Attributes (Properties): Characteristics or data that describe the object.
- Methods (Functions): Actions or behaviors that the object can perform.
Objects in OOP help organize code by modeling real-world entities. For example, consider a Phone, a Book, and a Dog:
- Phone Attributes:
version,charge,service provider. - Phone Methods:
make calls,receive calls,play games. - Book Attributes:
author,number of pages. - Book Methods:
open,close. - Dog Attributes:
name,age,breed. - Dog Methods:
eat,bark,sleep,play fetch.
This is the basic idea of an object: it is a collection of attributes and methods related to a specific real-world concept.
2. What is a Class?
A class is like a blueprint for creating objects. A class defines the attributes and methods that an object will have but does not create an object itself. To create an object, you use a class as a template.
For example, letβs consider a Human class:
class Human {
public:
string name;
string occupation;
int age;
void eat() {
std::cout << "This person is eating." << std::endl;
}
void drink() {
std::cout << "This person is drinking." << std::endl;
}
void sleep() {
std::cout << "This person is sleeping." << std::endl;
}
};
In this example:
- Attributes:
name,occupation,agerepresent the characteristics of a human. - Methods:
eat(),drink(),sleep()represent actions the human can perform.
3. Creating Objects from Classes
Once you define a class, you can create multiple objects of that class, each with its own unique set of attributes. For instance, to create a Human object, you use the following syntax:
Human human1; // Declare an object
human1.name = "Rick";
human1.occupation = "Scientist";
human1.age = 70;
This creates a human object named human1, with its own attributes (name, occupation, age) that can be independently set.
4. Invoking Methods on Objects
Objects can invoke methods defined in their class. For instance, the human1 object can perform the eat, drink, and sleep methods:
human1.eat(); // Output: This person is eating.
human1.drink(); // Output: This person is drinking.
human1.sleep(); // Output: This person is sleeping.
Here, the methods eat(), drink(), and sleep() perform actions for the human1 object.
5. Creating Multiple Objects
You can create multiple objects of the same class, each with its own unique attributes. For instance:
Human human2; // Declare another object
human2.name = "Morty";
human2.occupation = "Student";
human2.age = 15;
std::cout << "Human 2's name: " << human2.name << std::endl;
std::cout << "Human 2's occupation: " << human2.occupation << std::endl;
std::cout << "Human 2's age: " << human2.age << std::endl;
human2.eat();
human2.drink();
human2.sleep();
This creates a second object (human2), with its own set of attributes and methods.
6. Default Attribute Values
You can assign default values to attributes directly within the class, so when objects are created, they automatically start with certain values. For instance:
class Human {
public:
string name = "Rick"; // Default name
string occupation = "Scientist"; // Default occupation
int age = 70; // Default age
};
With this setup, when you create a Human object, its attributes will automatically be set to these default values unless you explicitly change them.
7. Example of Another Class: Car
Letβs look at another exampleβa Car class. A car object could have attributes like make, model, year, and color. It could also have methods like accelerate and brake.
class Car {
public:
string make;
string model;
int year;
string color;
void accelerate() {
std::cout << "You step on the gas." << std::endl;
}
void brake() {
std::cout << "You step on the brakes." << std::endl;
}
};
Here, the Car class has:
- Attributes:
make,model,year,color. - Methods:
accelerate(),brake().
To create a Car object and assign values to its attributes, you can do the following:
Car car1; // Create an object of Car class
car1.make = "Ford";
car1.model = "Mustang";
car1.year = 2023;
car1.color = "Silver";
// Display the car's attributes
std::cout << "Car make: " << car1.make << std::endl;
std::cout << "Car model: " << car1.model << std::endl;
std::cout << "Car year: " << car1.year << std::endl;
std::cout << "Car color: " << car1.color << std::endl;
// Invoke methods on the car object
car1.accelerate(); // Output: You step on the gas.
car1.brake(); // Output: You step on the brakes.
8. Summary of Key Concepts in OOP
- Object: A collection of attributes (data) and methods (functions).
- Class: A blueprint that defines the structure (attributes and methods) for creating objects.
- Attributes: Characteristics or data of an object (e.g.,
name,age). - Methods: Actions or behaviors an object can perform (e.g.,
eat(),sleep()). - Creating Objects: To create an object, use the class name and assign values to its attributes.
- Invoking Methods: Use the
dot notation(e.g.,object.method()) to call a method on an object.
9. Conclusion
In OOP, objects represent real-world entities with attributes and methods. Classes are blueprints for creating these objects. You can define attributes and methods inside a class, create objects based on that class, and invoke methods on those objects to perform actions. This concept allows for better organization, reusability, and maintainability in programming.
Assignment Suggestion:
Your assignment is to create your own class (e.g., representing a Student, Animal, or Smartphone) with relevant attributes and methods, and post your solution in the comments section.