Object Oriented Programming using C++
There are some basic concepts that act as the building blocks of OOPs i.e.  Class  Objects  Encapsulation  Abstraction  Polymorphism  Inheritance
Class  The building block of C++ that leads to Object-Oriented programming is a Class.  It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class.  A class is like a blueprint for an object.  For Example: Consider the Class of Cars. There may be many cars with different names and brands but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range, etc. So here, the Car is the class, and wheels, speed limits, and mileage are their properties.
Object  An Object is an identifiable entity with some characteristics and behavior.  An Object is an instance of a Class.  When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.  Object means a real word entity such as pen, chair, table etc.  Any entity that has state and behavior is known as an object.
Encapsulation  Binding (or wrapping) code and data together into a single unit is known as encapsulation.  For example: capsule, it is wrapped with different medicines.  Encapsulation is typically understood as the grouping of related pieces of information and data into a single entity.  Encapsulation is the process of tying together data and the functions that work with it in object-oriented programming.  Take a look at a practical illustration of encapsulation: at a company, there are various divisions, including the sales division, the finance division, and the accounts division.  All financial transactions are handled by the finance sector, which also maintains records of all financial data.  In a similar vein, the sales section is in charge of all tasks relating to sales and maintains a record of each sale.
Cont…..  Now, a scenario could occur when, for some reason, a financial official requires all the information on sales for a specific month.  Under the umbrella term "sales section," all of the employees who can influence the sales section's data are grouped together.  Data abstraction or concealing is another side effect of encapsulation.  In the same way that encapsulation hides the data.  In the aforementioned example, any other area cannot access any of the data from any of the sections, such as sales, finance, or accounts.
Abstraction  Hiding internal details and showing functionality is known as abstraction.  Data abstraction is the process of exposing to the outside world only the information that is absolutely necessary while concealing implementation or background information.  For example: phone call, we don't know the internal processing.  In C++, we use abstract class and interface to achieve abstraction.
Polymorphism  When one task is performed by different ways i.e. known as polymorphism.  For example: to convince the customer differently, to draw something e.g. shape or rectangle etc.  Different situations may cause an operation to behave differently.  The type of data utilized in the operation determines the behavior.
Inheritance  When one object acquires all the properties and behaviours of parent object i.e. known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.  Sub class - Subclass or Derived Class refers to a class that receives properties from another class.  Super class - The term "Base Class" or "Super Class" refers to the class from which a subclass inherits its properties.  Reusability - As a result, when we wish to create a new class, but an existing class already contains some of the code we need, we can generate our new class from the old class thanks to inheritance. This allows us to utilize the fields and methods of the pre-existing class.
There are mainly five types of Inheritance in C++ . They are as follows:  Single Inheritance  Multiple Inheritance  Multilevel Inheritance  Hierarchical Inheritance  Hybrid Inheritance
Single Inheritance  Single inheritance is defined as the inheritance in which a derived class is inherited from the only one base class. Where 'A' is the base class, and 'B' is the derived class.
Let's see the example of single level inheritance which inherits the fields only.  #include <iostream>  using namespace std;  class Account {  public:  float salary = 60000;  };  class Programmer: public Account {  public:  float bonus = 5000;  };  int main(void) {  Programmer p1;  cout<<"Salary: "<<p1.salary<<endl;  cout<<"Bonus: "<<p1.bonus<<endl;  return 0;  }
Let's see another example of inheritance in C++ which inherits methods only.  #include <iostream>  using namespace std;  class Animal {  public:  void eat() {  cout<<"Eating..."<<endl;  }  };  class Dog: public Animal  {  public:  void bark(){  cout<<"Barking...";  }  };  int main(void) {  Dog d1;  d1.eat();  d1.bark();  return 0;  }
C++ Multilevel Inheritance  Multilevel inheritance is a process of deriving a class from another derived class. When one class inherits another class which is further inherited by another class, it is known as multi level inheritance in C++. Inheritance is transitive so the last derived class acquires all the members of all its base classes.
Let's see the example of multi level inheritance in C++.  #include <iostream.h>  using namespace std;  class Animal {  public:  void eat() {  cout<<"Eating..."<<endl;  }  };  class Dog: public Animal  {   public:  void bark(){  cout<<"Barking..."<<endl;  }  };   class BabyDog: public Dog  {  public:  void weep() {   cout<<"Weeping...";  }  };  int main(void) {  BabyDog d1;  d1.eat();  d1.bark();  d1.weep();  return 0;  }
What Are Child and Parent classes?  To clearly understand the concept of Inheritance, you must learn about two terms on which the whole concept of inheritance is based - Child class and Parent class.  Child class: The class that inherits the characteristics of another class is known as the child class or derived class. The number of child classes that can be inherited from a single parent class is based upon the type of inheritance. A child class will access the data members of the parent class according to the visibility mode specified during the declaration of the child class.  Parent class: The class from which the child class inherits its properties is called the parent class or base class. A single parent class can derive multiple child classes (Hierarchical Inheritance) or multiple parent classes can inherit a single base class (Multiple Inheritance). This depends on the different types of inheritance in C++.
The syntax for defining the child class and parent class in all types of Inheritance in C++ is given below:  class parent_class  {  //class definition of the parent class  };  class child_class : visibility_mode parent_class  {  //class definition of the child class  };
Let's see the example of Polymorphism  #include <iostream>  using namespace std;  class Animal {  public:  void speed (){  Cout<<“who is more fastern”;  };  class Cheetah: public Animal {  public:  void speed(){  Cout<<“Cheetah says I’m fastern”;  };  int main(void) {  Class Dolphin: public Animal{  public:  void speed(){  Cout<<“Dolphin says im fastern”  }  Int main(){  Animal a;  Cheetah c;  Dolphin d;  a.speed();  C.speed();  D.speed();  Return 0;
Let's see the example of encapsulation in C++.  //program to calculate the area of a rectangle #include <iostream> Using namespace std; Class rectangle{ Public: //variables required for area calculation Int length; Int breadth;  //constructor to initialize variables Rectangle(int len, int brth):length(len),breadth(brth){} //Function to calculate area Int getArea(){ return length * breadth; } }; Int main(){ //create object of Rectangle class   Rectangle rect(8,6); //Call getArea() Function Cout<<Area=“<<rect.getArea(); Return 0; }
A C++ program demonstrating encapsulation with a Bank Account class #include <iostream> #include <string> Using namespace std; class BankAccount { private: string accountNumber; double balance; public: // Constructor BankAccount(string accNum, double initialBalance) { accountNumber = accNum; if (initialBalance >= 0) { balance = initialBalance; } else { balance = 0; // Initialize balance to 0 if initialBalance is negative cout << "Initial balance cannot be negative. Setting to 0." << endl; } }  // Public method to deposit funds void deposit(double amount) { if (amount > 0) { balance += amount; cout << "Deposited: " << amount << ". New balance: " << balance << endl; } else { cout << "Deposit amount must be positive." << endl; } } // Public method to withdraw funds void withdraw(double amount) { if (amount > 0 && amount <= balance) { balance -= amount; cout << "Withdrew: " << amount << ". New balance: " << balance << endl; } else if (amount <= 0) { cout << "Withdrawal amount must be positive." << endl; } else { cout << "Insufficient funds." << endl; } }   // Public method to get the account balance (getter) double getBalance() const { return balance; } // Public method to get the account number (getter) std::string getAccountNumber() const { return accountNumber; } }; int main() { // Create a BankAccount object BankAccount myAccount("123456789", 1000.0); // Access data and perform operations using public methods cout << "Account Number: " << myAccount.getAccountNumber() << endl; cout << "Current Balance: " << myAccount.getBalance() << endl; myAccount.deposit(500.0); myAccount.withdraw(200.0); myAccount.withdraw(1500.0 myAccount.deposit(-100.0); cout << "Final Balance: " << myAccount.getBalance() << endl; // myAccount.balance = 5000.0; return 0;

Object Oriented Programming using c++.pptx

  • 1.
  • 2.
    There are somebasic concepts that act as the building blocks of OOPs i.e.  Class  Objects  Encapsulation  Abstraction  Polymorphism  Inheritance
  • 3.
    Class  The buildingblock of C++ that leads to Object-Oriented programming is a Class.  It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class.  A class is like a blueprint for an object.  For Example: Consider the Class of Cars. There may be many cars with different names and brands but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range, etc. So here, the Car is the class, and wheels, speed limits, and mileage are their properties.
  • 4.
    Object  An Objectis an identifiable entity with some characteristics and behavior.  An Object is an instance of a Class.  When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.  Object means a real word entity such as pen, chair, table etc.  Any entity that has state and behavior is known as an object.
  • 5.
    Encapsulation  Binding (orwrapping) code and data together into a single unit is known as encapsulation.  For example: capsule, it is wrapped with different medicines.  Encapsulation is typically understood as the grouping of related pieces of information and data into a single entity.  Encapsulation is the process of tying together data and the functions that work with it in object-oriented programming.  Take a look at a practical illustration of encapsulation: at a company, there are various divisions, including the sales division, the finance division, and the accounts division.  All financial transactions are handled by the finance sector, which also maintains records of all financial data.  In a similar vein, the sales section is in charge of all tasks relating to sales and maintains a record of each sale.
  • 6.
    Cont…..  Now, ascenario could occur when, for some reason, a financial official requires all the information on sales for a specific month.  Under the umbrella term "sales section," all of the employees who can influence the sales section's data are grouped together.  Data abstraction or concealing is another side effect of encapsulation.  In the same way that encapsulation hides the data.  In the aforementioned example, any other area cannot access any of the data from any of the sections, such as sales, finance, or accounts.
  • 7.
    Abstraction  Hiding internaldetails and showing functionality is known as abstraction.  Data abstraction is the process of exposing to the outside world only the information that is absolutely necessary while concealing implementation or background information.  For example: phone call, we don't know the internal processing.  In C++, we use abstract class and interface to achieve abstraction.
  • 8.
    Polymorphism  When onetask is performed by different ways i.e. known as polymorphism.  For example: to convince the customer differently, to draw something e.g. shape or rectangle etc.  Different situations may cause an operation to behave differently.  The type of data utilized in the operation determines the behavior.
  • 9.
    Inheritance  When oneobject acquires all the properties and behaviours of parent object i.e. known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.  Sub class - Subclass or Derived Class refers to a class that receives properties from another class.  Super class - The term "Base Class" or "Super Class" refers to the class from which a subclass inherits its properties.  Reusability - As a result, when we wish to create a new class, but an existing class already contains some of the code we need, we can generate our new class from the old class thanks to inheritance. This allows us to utilize the fields and methods of the pre-existing class.
  • 10.
    There are mainlyfive types of Inheritance in C++ . They are as follows:  Single Inheritance  Multiple Inheritance  Multilevel Inheritance  Hierarchical Inheritance  Hybrid Inheritance
  • 11.
    Single Inheritance  Singleinheritance is defined as the inheritance in which a derived class is inherited from the only one base class. Where 'A' is the base class, and 'B' is the derived class.
  • 12.
    Let's see theexample of single level inheritance which inherits the fields only.  #include <iostream>  using namespace std;  class Account {  public:  float salary = 60000;  };  class Programmer: public Account {  public:  float bonus = 5000;  };  int main(void) {  Programmer p1;  cout<<"Salary: "<<p1.salary<<endl;  cout<<"Bonus: "<<p1.bonus<<endl;  return 0;  }
  • 13.
    Let's see anotherexample of inheritance in C++ which inherits methods only.  #include <iostream>  using namespace std;  class Animal {  public:  void eat() {  cout<<"Eating..."<<endl;  }  };  class Dog: public Animal  {  public:  void bark(){  cout<<"Barking...";  }  };  int main(void) {  Dog d1;  d1.eat();  d1.bark();  return 0;  }
  • 14.
    C++ Multilevel Inheritance Multilevel inheritance is a process of deriving a class from another derived class. When one class inherits another class which is further inherited by another class, it is known as multi level inheritance in C++. Inheritance is transitive so the last derived class acquires all the members of all its base classes.
  • 15.
    Let's see theexample of multi level inheritance in C++.  #include <iostream.h>  using namespace std;  class Animal {  public:  void eat() {  cout<<"Eating..."<<endl;  }  };  class Dog: public Animal  {   public:  void bark(){  cout<<"Barking..."<<endl;  }  };   class BabyDog: public Dog  {  public:  void weep() {   cout<<"Weeping...";  }  };  int main(void) {  BabyDog d1;  d1.eat();  d1.bark();  d1.weep();  return 0;  }
  • 16.
    What Are Childand Parent classes?  To clearly understand the concept of Inheritance, you must learn about two terms on which the whole concept of inheritance is based - Child class and Parent class.  Child class: The class that inherits the characteristics of another class is known as the child class or derived class. The number of child classes that can be inherited from a single parent class is based upon the type of inheritance. A child class will access the data members of the parent class according to the visibility mode specified during the declaration of the child class.  Parent class: The class from which the child class inherits its properties is called the parent class or base class. A single parent class can derive multiple child classes (Hierarchical Inheritance) or multiple parent classes can inherit a single base class (Multiple Inheritance). This depends on the different types of inheritance in C++.
  • 17.
    The syntax fordefining the child class and parent class in all types of Inheritance in C++ is given below:  class parent_class  {  //class definition of the parent class  };  class child_class : visibility_mode parent_class  {  //class definition of the child class  };
  • 18.
    Let's see theexample of Polymorphism  #include <iostream>  using namespace std;  class Animal {  public:  void speed (){  Cout<<“who is more fastern”;  };  class Cheetah: public Animal {  public:  void speed(){  Cout<<“Cheetah says I’m fastern”;  };  int main(void) {  Class Dolphin: public Animal{  public:  void speed(){  Cout<<“Dolphin says im fastern”  }  Int main(){  Animal a;  Cheetah c;  Dolphin d;  a.speed();  C.speed();  D.speed();  Return 0;
  • 19.
    Let's see theexample of encapsulation in C++.  //program to calculate the area of a rectangle #include <iostream> Using namespace std; Class rectangle{ Public: //variables required for area calculation Int length; Int breadth;  //constructor to initialize variables Rectangle(int len, int brth):length(len),breadth(brth){} //Function to calculate area Int getArea(){ return length * breadth; } }; Int main(){ //create object of Rectangle class   Rectangle rect(8,6); //Call getArea() Function Cout<<Area=“<<rect.getArea(); Return 0; }
  • 20.
    A C++ programdemonstrating encapsulation with a Bank Account class #include <iostream> #include <string> Using namespace std; class BankAccount { private: string accountNumber; double balance; public: // Constructor BankAccount(string accNum, double initialBalance) { accountNumber = accNum; if (initialBalance >= 0) { balance = initialBalance; } else { balance = 0; // Initialize balance to 0 if initialBalance is negative cout << "Initial balance cannot be negative. Setting to 0." << endl; } }  // Public method to deposit funds void deposit(double amount) { if (amount > 0) { balance += amount; cout << "Deposited: " << amount << ". New balance: " << balance << endl; } else { cout << "Deposit amount must be positive." << endl; } } // Public method to withdraw funds void withdraw(double amount) { if (amount > 0 && amount <= balance) { balance -= amount; cout << "Withdrew: " << amount << ". New balance: " << balance << endl; } else if (amount <= 0) { cout << "Withdrawal amount must be positive." << endl; } else { cout << "Insufficient funds." << endl; } }   // Public method to get the account balance (getter) double getBalance() const { return balance; } // Public method to get the account number (getter) std::string getAccountNumber() const { return accountNumber; } }; int main() { // Create a BankAccount object BankAccount myAccount("123456789", 1000.0); // Access data and perform operations using public methods cout << "Account Number: " << myAccount.getAccountNumber() << endl; cout << "Current Balance: " << myAccount.getBalance() << endl; myAccount.deposit(500.0); myAccount.withdraw(200.0); myAccount.withdraw(1500.0 myAccount.deposit(-100.0); cout << "Final Balance: " << myAccount.getBalance() << endl; // myAccount.balance = 5000.0; return 0;