Chapter 7: Implementating OOP in a Programming Language

Chapter 7: Implementating OOP in a Programming Language

Abstract:
Explanation

Class Definition:
      • class Animal { ... }: Defines a class named "Animal" with member variables and functions.
      • Access Modifiers:
        • public: Members declared as 'public' can be accessed from anywhere in the program. 
        • protected: Members declared as 'protected' can be accessed by the class itself and its derived classes 
        • private: Members declared as 'private' Jocan only be accessed within the class 
  • Inheritance:
    • class Dog : public Animal { ... }: Class "Dog" inherits from class "Animal", inheriting all its public and protected members. 
  • Polymorphism:
    • virtual void speak(): Declaring 'speak' as 'virtual' allows derived classes to override its behavior. 
    • void speak() override: In derived classes, 'override' keyword indicates that the method is intended to override a base class method. 
  • Constructor and Destructor:
    • Person(std::string n) : name(n): A constructor that takes a name as input and initializes the 'name' member variable. 
    • ~Person(): A destructor that is called when an object of type 'Person' is destroyed. 
Key Points:
  • Object Creation:
    To create an object of a class, use the new keyword followed by the class name and constructor arguments (e.g., Dog myDog = new Dog();). 
  • Accessing Members:
    Use the dot operator to access member variables and functions of an object (e.g., myDog.setName("Max");) 
  • Overriding Methods:
    When a derived class defines a method with the same name as a base class method, the derived class's method is called (polymorphism). 
So let's explore the chapter 7 in detail

Chapter 7: Implementating OOP in a Programming Language

7.1 Introduction

Programming languages such as C++ and Java provide robust mechanisms to implement Object-Oriented Programming (OOP) concepts. This chapter explores how to define classes and objects, use access modifiers, implement inheritance and polymorphism, and handle constructors and destructors in C++ and Java. Understanding these concepts is crucial for building scalable and maintainable software applications.


7.2 Syntax for Defining Classes and Objects

Classes and objects are fundamental building blocks of OOP. A class serves as a blueprint for objects, encapsulating data (attributes) and behaviors (methods).

7.2.1 Class and Object Definition in C++

In C++, a class is defined using the class keyword, and objects are created from the class.

Example: Defining a Class and Creating an Object in C++

#include <iostream>
using namespace std;

class Car {  
public:  
    string brand;  
    int year;  

    void display() {  
        cout << "Brand: " << brand << ", Year: " << year << endl;  
    }  
};  

int main() {  
    Car car1;  // Creating an object  
    car1.brand = "Toyota";  
    car1.year = 2022;  
    car1.display();  
    return 0;  
}

7.2.2 Class and Object Definition in Java

In Java, a class is defined using the class keyword, and objects are created using the new keyword.

Example: Defining a Class and Creating an Object in Java

class Car {  
    String brand;  
    int year;  

    void display() {  
        System.out.println("Brand: " + brand + ", Year: " + year);  
    }  
}  

public class Main {  
    public static void main(String[] args) {  
        Car car1 = new Car(); // Creating an object  
        car1.brand = "Toyota";  
        car1.year = 2022;  
        car1.display();  
    }  
}

7.3 Access Modifiers and Member Functions

Access modifiers control the visibility of class members. The three primary access modifiers in C++ and Java are:

  • public: Accessible from outside the class.
  • private: Accessible only within the class.
  • protected: Accessible within the class and its derived classes.

7.3.1 Access Modifiers in C++

#include <iostream>
using namespace std;

class Person {  
private:  
    int age;  

public:  
    void setAge(int a) {  
        age = a;  
    }  

    int getAge() {  
        return age;  
    }  
};  

int main() {  
    Person p1;  
    p1.setAge(25);  
    cout << "Age: " << p1.getAge() << endl;  
    return 0;  
}

7.3.2 Access Modifiers in Java

class Person {  
    private int age;  

    public void setAge(int a) {  
        age = a;  
    }  

    public int getAge() {  
        return age;  
    }  
}  

public class Main {  
    public static void main(String[] args) {  
        Person p1 = new Person();  
        p1.setAge(25);  
        System.out.println("Age: " + p1.getAge());  
    }  
}

7.4 Implementing Inheritance and Polymorphism

7.4.1 Inheritance

Inheritance enables a class to acquire properties and behaviors of another class, promoting code reusability.

Example: Inheritance in C++

#include <iostream>
using namespace std;

class Animal {  
public:  
    void makeSound() {  
        cout << "Animal makes a sound" << endl;  
    }  
};  

class Dog : public Animal {  
public:  
    void bark() {  
        cout << "Dog barks" << endl;  
    }  
};  

int main() {  
    Dog d;  
    d.makeSound();  
    d.bark();  
    return 0;  
}

Example: Inheritance in Java

class Animal {  
    void makeSound() {  
        System.out.println("Animal makes a sound");  
    }  
}  

class Dog extends Animal {  
    void bark() {  
        System.out.println("Dog barks");  
    }  
}  

public class Main {  
    public static void main(String[] args) {  
        Dog d = new Dog();  
        d.makeSound();  
        d.bark();  
    }  
}

7.4.2 Polymorphism

Polymorphism allows methods to be overridden in a derived class, enabling dynamic behavior.

Example: Polymorphism in C++ (Method Overriding)

#include <iostream>
using namespace std;

class Animal {  
public:  
    virtual void makeSound() {  
        cout << "Animal makes a sound" << endl;  
    }  
};  

class Dog : public Animal {  
public:  
    void makeSound() override {  
        cout << "Dog barks" << endl;  
    }  
};  

int main() {  
    Animal* a;  
    Dog d;  
    a = &d;  
    a->makeSound();  
    return 0;  
}

Example: Polymorphism in Java (Method Overriding)

class Animal {  
    void makeSound() {  
        System.out.println("Animal makes a sound");  
    }  
}  

class Dog extends Animal {  
    @Override  
    void makeSound() {  
        System.out.println("Dog barks");  
    }  
}  

public class Main {  
    public static void main(String[] args) {  
        Animal a = new Dog();  
        a.makeSound();  
    }  
}

7.5 Handling Constructors and Destructors

7.5.1 Constructors in C++ and Java

A constructor is a special method called when an object is created.

Example: Constructor in C++

#include <iostream>
using namespace std;

class Car {  
public:  
    string brand;  

    Car(string b) {  
        brand = b;  
    }  

    void display() {  
        cout << "Brand: " << brand << endl;  
    }  
};  

int main() {  
    Car car1("Toyota");  
    car1.display();  
    return 0;  
}

Example: Constructor in Java

class Car {  
    String brand;  

    Car(String b) {  
        brand = b;  
    }  

    void display() {  
        System.out.println("Brand: " + brand);  
    }  
}  

public class Main {  
    public static void main(String[] args) {  
        Car car1 = new Car("Toyota");  
        car1.display();  
    }  
}

7.5.2 Destructors in C++

In C++, a destructor is defined using the ~ symbol and is automatically invoked when an object goes out of scope.

#include <iostream>
using namespace std;

class Car {  
public:  
    Car() {  
        cout << "Car created" << endl;  
    }  

    ~Car() {  
        cout << "Car destroyed" << endl;  
    }  
};  

int main() {  
    Car car1;  
    return 0;  
}

(Java does not have destructors, but it has a garbage collector that automatically frees memory.)


7.6 Summary

This chapter covered essential OOP concepts in C++ and Java, including defining classes and objects, access modifiers, inheritance, polymorphism, constructors, and destructors. These concepts are vital for writing efficient, reusable, and maintainable code in modern programming.

 

Comments