Oop Finals Reviewer
Oop Finals Reviewer
In this example, the Car class encapsulates the details of the car (model and year). The private
attributes (model and year) can only be accessed and modified through public methods
(setDetails and displayDetails), providing a controlled interface to the outside world.
Inheratance
Here, the Mammal class inherits from the Animal class. It can use the eat() method from the
base class and also has its own method (giveBirth). Inheritance allows for code reuse and the
creation of a hierarchy.
Polymorphism
The provided C++ code illustrates the concept of polymorphism through a hierarchy of classes
representing different shapes. Three classes—Shape, Rectangle, and Circle—are defined, each
with a draw() method. The draw() method is overridden in the subclasses to provide specific
implementations. In the main function, objects of Circle and Rectangle are created, and despite
both objects having a draw() method, the actual behavior is determined by the type of the object
at runtime. This demonstrates the polymorphic nature of the code, where different objects can be
treated uniformly through a common interface, allowing for flexibility and code reuse.
Abstraction
The RemoteControl class declares an abstract method pressButton. The TVRemote class,
which inherits from RemoteControl, provides a concrete implementation. Users of
RemoteControl only need to know about the abstract method, abstracting away the details of
how each remote control works internally.
You should be familiar with the following codes to identify what specific property is
being applied.
1. Encapsulation
Key Codes: Private and Public Access Specifiers: private, public, protected
Getter and Setter Methods
2. Inheritance:
Key Codes:
class keyword
public, protected, private access specifiers
: public BaseClass (or protected, private) for inheritance
3. Polymorphism:
Key Codes:
virtual keyword for declaring virtual methods
override keyword for indicating overridden methods in derived classes
4. Abstraction:
Key Codes:
virtual methods in abstract classes
= 0 (pure virtual function) for creating abstract classes
Other terms you should be familiar with
Class: A blueprint or template for creating objects. It defines the properties (attributes) and
behaviors (methods) that objects created from the class will have.
Example: In C++, class MyClass { /* class members */ };
Object: An instance of a class. It is a concrete realization of the properties and behaviors
defined by a class.
Example: MyClass obj;
Method: A function associated with an object or a class. Methods define the behaviors that
objects of a class can exhibit.
Example: void myMethod() { /* method implementation */ }
Constructor: A special method that is automatically called when an object is created. It
initializes the object's state.
Example: In C++, MyClass() { /* constructor code */ }
Destructor: A special method that is automatically called when an object is destroyed. It is
responsible for releasing resources or performing cleanup.
Example: In C++, ~MyClass() { /* destructor code */ }
Attribute/Member Variable/Field: A data member of a class that represents the state or
characteristics of an object.
Example: int myAttribute;
Encapsulation: The bundling of data (attributes) and methods that operate on the data into a
single unit (class). It restricts direct access to some of an object's components.
Example: Private and public access specifiers in C++ classes.
Inheritance: The mechanism by which a new class (subclass/derived class) inherits properties
and behaviors from an existing class (superclass/base class). It promotes code reuse.
Example: class DerivedClass : public BaseClass { /* class members */ };
Polymorphism: The ability of a single function or method to operate on different types of
objects. It includes compile-time (overloading) and runtime (overriding) polymorphism.
Example: Overriding a virtual method in a derived class.
Abstraction: The process of hiding the complex implementation details of an object and
exposing only the essential features. It focuses on what an object does rather than how it
achieves its functionality.
Example: Declaring an abstract class or interface in C++.
Method Overloading: Defining multiple methods in the same class with the same name but
different parameter lists.
Example: void myMethod(int x); and void myMethod(double y);
Method Overriding: Providing a specific implementation for a method in a subclass that is
already defined in its superclass.
Example: Overriding the draw() method in a derived class.