Object-Oriented Programming Notes
Object-Oriented Programming Notes
Inheritance
Mechanism by which one class can derive or inherit its
features from another class.
Polymorphism
The same function or operator behaves differently based
on the object or context.
Two types of Polymorphism:
1.Method overloading/ function overloading is decided
at compile time.
#include <iostream>
#include <string>
class Engine {
public:
Engine() {
~Engine() {
};
class Car {
private:
std::string model;
public:
Car(std::string m) : model(m) {
std::cout << "Car '" << model << "' built." << std::endl;
~Car() {
std::cout << "Car '" << model << "' is scrapped." <<
std::endl;
};
int main() {
Car myCar("SimpleCar");
myCar.drive();
return 0;
#include <iostream>
#include <string>
class Book {
public:
std::string title;
Book(std::string t) : title(t) {
std::cout << "Book '" << title << "' exists." << std::endl;
~Book() {
std::cout << "Book '" << title << "' is gone." << std::endl;
};
class Student {
public:
std::string name;
std::cout << "Student '" << name << "' created." <<
std::endl;
~Student() {
std::cout << "Student '" << name << "' is done." <<
std::endl;
currentBook = book;
std::cout << name << " borrowed '" << book->title << "'."
<< std::endl;
}
void study() const {
if (currentBook) {
currentBook->read();
} else {
};
int main() {
Student student1("Alice");
student1.borrowBook(book1);
student1.study();
Student student2("Bob");