knowt logo

Object-Orientated Programming (IB)

Principles of object-oriented programming:

Encapsulation: 

  • Encapsulation is the wrapping of data and functions together as a single unit. 

    • By default, data is not accessible to the outside world and they are only accessible through the functions which are wrapped in a class. 

    • Prevention of data direct access by the program is called data hiding or information hiding

Data abstraction:

  • Abstraction refers to the act of representing essential features without including the background details or explanation. 

    • Classes use the concept of abstraction and are defined as a list of attributes such as size, weight, cost and functions to operate on these attributes. 

    • They encapsulate all essential properties of the object that are to be created. 

      • The attributes are called data members as they hold data, and the functions that operate on these data are called member functions.

    • Class use the concept of data abstraction so they are called abstract data type (ADT)

Polymorphism: 

  • Polymorphism comes from the Greek words “poly” and “morphism”. 

    • “Poly” means many and “morphism” means form i.e. many forms. 

  • Polymorphism means the ability to take more than one form. 

    • For example, an operation has different behaviour in different instances. 

  • The behaviour depends upon the type of data used in the operation.

  • Different ways to achieve polymorphism in C++ programs:

    • Function overloading 

    • Operator overloading

Inheritance:

  • Inheritance is the process by which one object can acquire the properties of another.

    • Inheritance is the most promising concept of OOP, which helps realise the goal of constructing software from reusable parts, rather than hand-coding every system from scratch. 

  • Inheritance supports reuse across systems and directly facilitates extensibility within a system. Inheritance coupled with polymorphism and dynamic binding minimises the existing code to be modified while enhancing a system.

  • When the class child, inherits the class parent, the class child is referred to as a derived class (sub-class) and the class parent as a base class (superclass). 

  • In this case, the class child has two parts: 

    • a derived part 

    • an incremental part. 

      • The derived part is inherited from the class parent. 

      • The incremental part is the new code written specifically for the class child.

Dynamic Binding:

  • Binding refers to linking of procedure call to the code to be executed in response to the call.

  • Dynamic binding(or late binding) means the code associated with a given procedure call in not known until the time of call at run time.

Message passing:

  • An object-oriented program consists of a set of objects that communicate with each other.

    • Objects communicate with each other by sending and receiving information.

    • A message for an object is a request for the execution of a procedure and therefore invoke the function that is called for an object and generates the result.

Benefits of object-oriented programming (OOPs):

  • Reusability: In OOPs programs functions and modules that are written by a user can be reused by other users without any modification.

  • Inheritance: Through this we can eliminate redundant code and extend the use of existing classes.

  • Data Hiding: The programmer can hide the data and functions in a class from other classes. It helps the programmer to build the secure programs.

  • Reduced complexity of a problem: The given problem can be viewed as a collection of different objects. Each object is responsible for a specific task. 

    • The problem is solved by interfacing the objects. 

    • This technique reduces the complexity of the program design.

  • Easy to Maintain and Upgrade: OOP makes it easy to maintain and modify existing code as new objects can be created with small differences to existing ones. 

    • Software complexity can be easily managed.

  •  Message Passing: The technique of message communication between objects makes the interface with external systems easier.

  • Modifiability: It is easy to make minor changes in the data representation or the procedures in an

  • OO program. Changes inside a class do not affect any other part of a program, since the only

public interface that the external world has to a class is through the use of methods.

Concepts in OOPs:

Class:

  • A class is a user-defined data type. 

    • It consists of data members and member functions, which can be accessed and used by creating an instance of that class. 

    • It represents the set of properties or methods common to all objects of one type. 

    • A class is like a blueprint for an object.

Defining the Class:

  • Here’s the definition (sometimes called a specifier) for the class smallobj, copied from the SMALLOBJ listing:

class smallobj //define a class
{
private:
int somedata; //class data
public:
void setdata(int d) //member function to set data
{ somedata = d; }
void showdata() //member function to display data
{ cout << “\nData is “ << somedata; }
};
  • The definition starts with the keyword class, followed by the class name—smallobj in  this example. 

    • Like a structure, the body of the class is delimited by braces and terminated by a semicolon. 

    • (Don’t forget the semicolon. Remember, data constructs such as structures and classes end with a semicolon, while control constructs such as functions and loops do not.)

Access Specifiers:

Private and Public:

  • A key feature of object-oriented programming is data hiding. 

  • This term does not refer to the activities of particularly paranoid programmers; rather it means that data is concealed within a class so that it cannot be accessed mistakenly by functions outside the class. 

  • The primary mechanism for hiding data is to put it in a class and make it private. Private data or functions can only be accessed from within the class. 

  • Public data or functions, on the other hand, are accessible from outside the class.

Class Data:

  • The smallobj class contains one data item: somedata, which is of type int. 

  • The data items within a class are called data members (or sometimes member data). 

    • There can be any number of data members in a class, just as there can be any number of data items in a structure. 

    • The data member somedata follows the keyword private, so it can be accessed from within the class, but not from outside.

Object:

  • An object is an instance of a class. It is a self-contained entity that consists of both data (attributes) and procedures (methods) that operate on the data. 

  • Objects are the fundamental building blocks of Object-Oriented Programming (OOP). 

  • Each object can represent a real-world entity or concept, encapsulating its state and behavior.

    class Car:
    def __init__(self, make, model, year):
    self.make = make
    self.model = model
    self.year = year

    def start_engine(self):
    print(f"The {self.make} {self.model}'s engine has started.")

    my_car = Car('Toyota', 'Corolla', 2020)
    • In this example, my_car is an object of the Car class.

Method:

  • A method is a function that is defined within a class and is associated with the objects of that class. 

    • Methods describe the behaviors and actions that an object can perform. 

    • They can manipulate the object's attributes and interact with other objects.

Attribute:

  • An attribute is a variable that is bound to an instance of a class. 

  • Attributes hold data that is associated with an object. 

    • They represent the state or properties of the object. Attributes are defined within the class and are accessed using dot notation.

  • Example:

class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year

my_car = Car('Toyota', 'Corolla', 2020)
print(my_car.make) # Output: Toyota
print(my_car.model) # Output: Corolla
print(my_car.year) # Output: 2020
  • In this example, make, model, and year are attributes of the Car class.

  • These definitions and examples illustrate the core concepts of objects, methods, and attributes in Object-Oriented Programming, highlighting how they interact to form the basis of OOP design.

Object-Orientated Programming (IB)

Principles of object-oriented programming:

Encapsulation: 

  • Encapsulation is the wrapping of data and functions together as a single unit. 

    • By default, data is not accessible to the outside world and they are only accessible through the functions which are wrapped in a class. 

    • Prevention of data direct access by the program is called data hiding or information hiding

Data abstraction:

  • Abstraction refers to the act of representing essential features without including the background details or explanation. 

    • Classes use the concept of abstraction and are defined as a list of attributes such as size, weight, cost and functions to operate on these attributes. 

    • They encapsulate all essential properties of the object that are to be created. 

      • The attributes are called data members as they hold data, and the functions that operate on these data are called member functions.

    • Class use the concept of data abstraction so they are called abstract data type (ADT)

Polymorphism: 

  • Polymorphism comes from the Greek words “poly” and “morphism”. 

    • “Poly” means many and “morphism” means form i.e. many forms. 

  • Polymorphism means the ability to take more than one form. 

    • For example, an operation has different behaviour in different instances. 

  • The behaviour depends upon the type of data used in the operation.

  • Different ways to achieve polymorphism in C++ programs:

    • Function overloading 

    • Operator overloading

Inheritance:

  • Inheritance is the process by which one object can acquire the properties of another.

    • Inheritance is the most promising concept of OOP, which helps realise the goal of constructing software from reusable parts, rather than hand-coding every system from scratch. 

  • Inheritance supports reuse across systems and directly facilitates extensibility within a system. Inheritance coupled with polymorphism and dynamic binding minimises the existing code to be modified while enhancing a system.

  • When the class child, inherits the class parent, the class child is referred to as a derived class (sub-class) and the class parent as a base class (superclass). 

  • In this case, the class child has two parts: 

    • a derived part 

    • an incremental part. 

      • The derived part is inherited from the class parent. 

      • The incremental part is the new code written specifically for the class child.

Dynamic Binding:

  • Binding refers to linking of procedure call to the code to be executed in response to the call.

  • Dynamic binding(or late binding) means the code associated with a given procedure call in not known until the time of call at run time.

Message passing:

  • An object-oriented program consists of a set of objects that communicate with each other.

    • Objects communicate with each other by sending and receiving information.

    • A message for an object is a request for the execution of a procedure and therefore invoke the function that is called for an object and generates the result.

Benefits of object-oriented programming (OOPs):

  • Reusability: In OOPs programs functions and modules that are written by a user can be reused by other users without any modification.

  • Inheritance: Through this we can eliminate redundant code and extend the use of existing classes.

  • Data Hiding: The programmer can hide the data and functions in a class from other classes. It helps the programmer to build the secure programs.

  • Reduced complexity of a problem: The given problem can be viewed as a collection of different objects. Each object is responsible for a specific task. 

    • The problem is solved by interfacing the objects. 

    • This technique reduces the complexity of the program design.

  • Easy to Maintain and Upgrade: OOP makes it easy to maintain and modify existing code as new objects can be created with small differences to existing ones. 

    • Software complexity can be easily managed.

  •  Message Passing: The technique of message communication between objects makes the interface with external systems easier.

  • Modifiability: It is easy to make minor changes in the data representation or the procedures in an

  • OO program. Changes inside a class do not affect any other part of a program, since the only

public interface that the external world has to a class is through the use of methods.

Concepts in OOPs:

Class:

  • A class is a user-defined data type. 

    • It consists of data members and member functions, which can be accessed and used by creating an instance of that class. 

    • It represents the set of properties or methods common to all objects of one type. 

    • A class is like a blueprint for an object.

Defining the Class:

  • Here’s the definition (sometimes called a specifier) for the class smallobj, copied from the SMALLOBJ listing:

class smallobj //define a class
{
private:
int somedata; //class data
public:
void setdata(int d) //member function to set data
{ somedata = d; }
void showdata() //member function to display data
{ cout << “\nData is “ << somedata; }
};
  • The definition starts with the keyword class, followed by the class name—smallobj in  this example. 

    • Like a structure, the body of the class is delimited by braces and terminated by a semicolon. 

    • (Don’t forget the semicolon. Remember, data constructs such as structures and classes end with a semicolon, while control constructs such as functions and loops do not.)

Access Specifiers:

Private and Public:

  • A key feature of object-oriented programming is data hiding. 

  • This term does not refer to the activities of particularly paranoid programmers; rather it means that data is concealed within a class so that it cannot be accessed mistakenly by functions outside the class. 

  • The primary mechanism for hiding data is to put it in a class and make it private. Private data or functions can only be accessed from within the class. 

  • Public data or functions, on the other hand, are accessible from outside the class.

Class Data:

  • The smallobj class contains one data item: somedata, which is of type int. 

  • The data items within a class are called data members (or sometimes member data). 

    • There can be any number of data members in a class, just as there can be any number of data items in a structure. 

    • The data member somedata follows the keyword private, so it can be accessed from within the class, but not from outside.

Object:

  • An object is an instance of a class. It is a self-contained entity that consists of both data (attributes) and procedures (methods) that operate on the data. 

  • Objects are the fundamental building blocks of Object-Oriented Programming (OOP). 

  • Each object can represent a real-world entity or concept, encapsulating its state and behavior.

    class Car:
    def __init__(self, make, model, year):
    self.make = make
    self.model = model
    self.year = year

    def start_engine(self):
    print(f"The {self.make} {self.model}'s engine has started.")

    my_car = Car('Toyota', 'Corolla', 2020)
    • In this example, my_car is an object of the Car class.

Method:

  • A method is a function that is defined within a class and is associated with the objects of that class. 

    • Methods describe the behaviors and actions that an object can perform. 

    • They can manipulate the object's attributes and interact with other objects.

Attribute:

  • An attribute is a variable that is bound to an instance of a class. 

  • Attributes hold data that is associated with an object. 

    • They represent the state or properties of the object. Attributes are defined within the class and are accessed using dot notation.

  • Example:

class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year

my_car = Car('Toyota', 'Corolla', 2020)
print(my_car.make) # Output: Toyota
print(my_car.model) # Output: Corolla
print(my_car.year) # Output: 2020
  • In this example, make, model, and year are attributes of the Car class.

  • These definitions and examples illustrate the core concepts of objects, methods, and attributes in Object-Oriented Programming, highlighting how they interact to form the basis of OOP design.