Object Oriented Programming Using C++ (UNIT 1)
Object Oriented Programming Using C++ (UNIT 1)
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
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
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) {
Cont….
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(){
Cont….
cout<<"Barking...";
}
};
int main(void) {
Dog d1;
d1.eat();
d1.bark();
return 0;
}
C++ Multilevel Inheritance
public:
void bark(){
cout<<"Barking..."<<endl;
}
};
cout<<"Weeping...";
}
};
int main(void) {
BabyDog d1;
d1.eat();
d1.bark();
d1.weep();
return 0;
}
C++ Multiple Inheritance
Multiple inheritance is the process of deriving a new class that inherits the
attributes from two or more classes.
Let's see a simple example of multiple
inheritance.
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a(int n)
{
a = n;
}
};
Cont….
class B
{
protected:
int b;
public:
void get_b(int n)
{
b = n;
}
};
Cont…..
{
std::cout << "The value of a is : " <<a<< std::endl;
std::cout << "The value of b is : " <<b<< std::endl;
cout<<"Addition of a and b is : "<<a+b;
}
};
int main()
{
Cont….
C c;
c.get_a(10);
c.get_b(20);
c.display();
return 0;
}
C++ Hybrid Inheritance
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a()
{
std::cout << "Enter the value of 'a' : " << std::endl;
cin>>a;
}
};
Cont….
class B : public A
{
protected:
int b;
public:
void get_b()
{
std::cout << "Enter the value of 'b' : " << std::endl;
cin>>b;
}
};
Cont….
class C
{
protected:
int c;
public:
void get_c()
{
std::cout << "Enter the value of c is : " << std::endl;
cin>>c;
}
};
Cont…..
int main()
{
D d;
d.mul();
return 0;
}
Output
class A
{
// body of the class A.
}
class B : public A
{
// body of class B.
}
class C : public A
Cont….
{
// body of class C.
}
class D : public A
{
// body of class D.
}
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
};
Syntax Description
One of two important concepts that are provided by Object-Oriented Programming is the
concept of inheritance.
Through inheritance, the same attributes of a class are not required to be written repeatedly.
This avoids the issues where the same code has still to be written multiple times in a code.
With the introduction of the concept of classes, the code section can be used as many times
as required in the program.
Through the inheritance approach, a child class is created that inherits the fields and methods
of the parent class.
The methods and values that are present in the parent class can be easily overridden.
Through inheritance, the features of one class can be inherited by another class by extending
the class.
Therefore, inheritance is vital for providing code reusability and also multilevel inheritance.
3. Productivity
By the term data redundancy, it means that the data is repeated twice.
This means that the same data is present more than one time.
In Object-Oriented programming the data redundancy is considered to be an
advantage.
For example, the user wants to have a functionality that is similar to almost
all the classes.
In such cases, the user can create classes with similar functionaries and
inherit them wherever required.
5. Code Flexibility
Problems can be efficiently solved by breaking down the problem into smaller
pieces and this makes as one of the big advantages of object-oriented
programming.
If a complex problem is broken down into smaller pieces or components, it
becomes a good programming practice.
Considering this fact, OOPS utilizes this feature where it breaks down the
code of the software into smaller pieces of the object into bite-size pieces
that are created one at a time.
Once the problem is broken down, these broken pieces can be used again to
solve other problems.
Different element of C++
Tokens
A C++ program is composed of tokens which are the smallest individual
unit. Tokens can be one of several things, including keywords, identifiers,
constants, operators, or punctuation marks.
There are 6 types of tokens in c++:
Keyword
Identifiers
Constants
Strings
Special symbols
Operators
Keywords
In C++, keywords are reserved words that have a specific meaning in the
language and cannot be used as identifiers.
Keywords are an essential part of the C++ language and are used to perform
specific tasks or operations.
There are 95 keywords in c++.
If you try to use a reserved keyword as an identifier, the compiler will
produce an error because it will not know what to do with the keyword.
Indentifiers
Constants refer to fixed values that the program may not alter and they are
called literals.
Constants can be of any of the basic data types and can be divided into
Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean
Values.
Constants are treated just like regular variables except that their values
cannot be modified after their definition.
Expression
int main() {
string greeting = "Hello";
cout << greeting;
return 0;
}
THANK YOU