Absolutely! Object-Oriented Program
Absolutely! Object-Oriented Program
Abstraction: Hiding complex implementation details and exposing only the necessary
features.
Inheritance: Creating new classes (derived classes) from existing ones (base
classes) to reuse code.
Example:
cpp
Copy
#include <iostream>
using namespace std;
// Define a class
class Dog {
private:
string name; // Data member (attribute)
int age;
public:
// Member function to set data
void setData(string n, int a) {
name = n;
age = a;
}
int main() {
// Create an object of the Dog class
Dog myDog;
// Display data
myDog.display();
return 0;
}
Output:
Copy
Name: Buddy, Age: 3
3. Encapsulation
Encapsulation is achieved by using access specifiers:
In the example above, name and age are private, so they cannot be accessed directly
outside the class. Instead, we use public methods (setData and display) to interact
with them.
Example:
cpp
Copy
class Dog {
private:
string name;
int age;
public:
// Constructor
Dog(string n, int a) {
name = n;
age = a;
cout << "Dog object created!" << endl;
}
// Destructor
~Dog() {
cout << "Dog object destroyed!" << endl;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Dog myDog("Buddy", 3); // Constructor is called
myDog.display();
return 0; // Destructor is called when myDog goes out of scope
}
Output:
Copy
Dog object created!
Name: Buddy, Age: 3
Dog object destroyed!
5. Inheritance
Inheritance allows a class to inherit properties and behaviors from another class.
Example:
cpp
Copy
// Base class
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
// Derived class
class Dog : public Animal {
public:
void bark() {
cout << "Barking..." << endl;
}
};
int main() {
Dog myDog;
myDog.eat(); // Inherited from Animal
myDog.bark(); // Defined in Dog
return 0;
}
Output:
Copy
Eating...
Barking...
6. Polymorphism
Polymorphism allows functions or methods to behave differently based on the object
that calls them. It is achieved through function overriding and virtual functions.
Example:
cpp
Copy
class Animal {
public:
virtual void sound() {
cout << "Animal sound" << endl;
}
};
int main() {
Animal* myAnimal;
Dog myDog;
Cat myCat;
myAnimal = &myDog;
myAnimal->sound(); // Output: Woof!
myAnimal = &myCat;
myAnimal->sound(); // Output: Meow!
return 0;
}
Output:
Copy
Woof!
Meow!
7. Putting It All Together
Here’s a complete example combining all the concepts:
cpp
Copy
#include <iostream>
using namespace std;
// Base class
class Animal {
private:
string name;
public:
Animal(string n) : name(n) {
cout << "Animal created: " << name << endl;
}
~Animal() {
cout << "Animal destroyed: " << name << endl;
}
};
// Derived class
class Dog : public Animal {
public:
Dog(string n) : Animal(n) {}
int main() {
Dog myDog("Buddy");
myDog.sound();
return 0;
}
Output:
Copy
Animal created: Buddy
Woof!
Animal destroyed: Buddy
Next Steps
Practice creating classes and objects.
Explore advanced topics like templates, operator overloading, and STL (Standard
Template Library).
1. What is OOP?
OOP is a way of writing programs by organizing code into objects. An object is like
a real-world entity that has:
cpp
Copy
#include <iostream> // For input/output
using namespace std; // To avoid typing std:: before every command
int main() {
cout << "Hello, World!" << endl; // Print to the console
return 0; // End of program
}
Explanation:
using namespace std;: Lets us use cout and endl without typing std::.
// Define a class
class Dog {
public:
string name; // Property (data member)
int age;
int main() {
// Create an object of the Dog class
Dog myDog;
// Set properties
myDog.name = "Buddy";
myDog.age = 3;
// Call behavior
myDog.bark();
return 0;
}
Output:
Copy
Buddy says Woof!
Explanation:
4. Access Specifiers
Access specifiers control who can access the properties and behaviors of a class:
protected: Similar to private but can also be accessed by derived classes (we’ll
cover this later).
Example:
cpp
Copy
class Dog {
private:
string name; // Private property
public:
void setName(string n) {
name = n; // Set the private property
}
void bark() {
cout << name << " says Woof!" << endl;
}
};
int main() {
Dog myDog;
myDog.setName("Buddy"); // Set the name using a public function
myDog.bark(); // Call the public function
Copy
Buddy says Woof!
5. Constructors
A constructor is a special function that is automatically called when an object is
created. It is used to initialize properties.
Example:
cpp
Copy
class Dog {
private:
string name;
int age;
public:
// Constructor
Dog(string n, int a) {
name = n;
age = a;
cout << name << " is born!" << endl;
}
void bark() {
cout << name << " says Woof!" << endl;
}
};
int main() {
Dog myDog("Buddy", 3); // Constructor is called
myDog.bark();
return 0;
}
Output:
Copy
Buddy is born!
Buddy says Woof!
6. Putting It All Together
Here’s a complete example combining everything:
cpp
Copy
#include <iostream>
using namespace std;
class Dog {
private:
string name;
int age;
public:
// Constructor
Dog(string n, int a) {
name = n;
age = a;
}
// Behavior
void bark() {
cout << name << " says Woof!" << endl;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Dog myDog("Buddy", 3); // Create an object
myDog.display(); // Display properties
myDog.bark(); // Call behavior
return 0;
}
Output:
Copy
Name: Buddy, Age: 3
Buddy says Woof!
7. Practice Problems
Create a Car class with properties like model, color, and speed. Add behaviors like
accelerate() and brake().
Create a Person class with properties like name and age. Add a behavior like
introduce() to print "Hello, my name is [name]."