0% found this document useful (0 votes)
45 views54 pages

Object Oriented Programming Using C++ (UNIT 1)

The document provides an introduction to object-oriented programming (OOP) concepts using C++. It defines key OOP concepts like class, object, encapsulation, inheritance, polymorphism and abstraction. It then provides examples of different types of inheritance in C++ including single, multilevel, multiple and hybrid inheritance with code samples. The document is intended to teach basic OOP concepts and how they are implemented in C++.

Uploaded by

hp915396
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
45 views54 pages

Object Oriented Programming Using C++ (UNIT 1)

The document provides an introduction to object-oriented programming (OOP) concepts using C++. It defines key OOP concepts like class, object, encapsulation, inheritance, polymorphism and abstraction. It then provides examples of different types of inheritance in C++ including single, multilevel, multiple and hybrid inheritance with code samples. The document is intended to teach basic OOP concepts and how they are implemented in C++.

Uploaded by

hp915396
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 54

Object Oriented

Programming using C++


UNIT 1
BY:Ms SURBHI SAROHA
SYLLABUS

 Introduction to OOPs and C++ Elements


 Introduction to OOPs
 Features & Advantages of OOPs
 Different element of C++(Tokens, Keywords , Indentifiers , variable, constant
operators , Expression , String).
Introduction to OOPs and C++ Elements

 Object-oriented programming – As the name suggests uses objects in programming.


 Object-oriented programming aims to implement real-world entities like
inheritance, hiding, polymorphism, etc. in programming.
 The major purpose of C++ programming is to introduce the concept of object
orientation to the C programming language.
 Object Oriented Programming is a paradigm that provides many concepts such
as inheritance, data binding, polymorphism etc.
 The programming paradigm where everything is represented as an object is known
as truly object-oriented programming language.
 Smalltalk is considered as the first truly object-oriented programming language.
 Object-Oriented Programming is a methodology or paradigm to design a program
using classes and objects.
There are some basic concepts that act
as the building blocks of OOPs i.e.
 Class
 Objects
 Encapsulation
 Abstraction
 Polymorphism
 Inheritance
Characteristics of an Object-Oriented
Programming
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) {
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

 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
 {

Cont….

 public:
 void bark(){
 cout<<"Barking..."<<endl;
 }
 };

 class BabyDog: public Dog


 {
 public:
 void weep() {

Cont….

 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…..

 class C : public A,public B


 {
 public:
 void display()

 {
 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

 Hybrid inheritance is a combination of more than one type of inheritance.


Let's see a simple example:

 #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…..

 class D : public B, public C


 {
 protected:
 int d;
 public:
 void mul()
 {
 get_a();
 get_b();
 get_c();
 std::cout << "Multiplication of a,b,c is : " <<a*b*c<< std::endl;
 }
 };
Cont….

 int main()
 {
 D d;
 d.mul();
 return 0;
 }
Output

 Enter the value of 'a' :


 10
 Enter the value of 'b' :
 20
 Enter the value of c is :
 30
 Multiplication of a,b,c is : 6000
C++ Hierarchical Inheritance

 Hierarchical inheritance is defined as the process of deriving more than one


class from a base class.
Syntax of Hierarchical inheritance:

 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

 parent_class: Name of the base class or the parent class.


 child_class: Name of the derived class or the child class.
 visibility_mode: Type of the visibility mode (i.e., private, protected, and
public) that specifies how the data members of the child class inherit from
the parent class.
Advantages of OOPs

 1. Troubleshooting is easier with the OOP language


 Suppose the user has no idea where the bug lies if there is an error within the
code.
 Also, the user has no idea where to look into the code to fix the error.
 This is quite difficult for standard programming languages.
 However, when Object-Oriented Programming is applied, the user knows
exactly where to look into the code whenever there is an error.
 There is no need to check other code sections as the error will show where
the trouble lies.
2. Code Reusability

 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

 The productivity of two codes increases through the use of Object-Oriented


Programming.
 This is because the OOP has provided so many libraries that new programs
have become more accessible.
 Also, as it provides the facility of code reusability, the length of a code is
decreased, further enhancing the faster development of newer codes and
programs.
4. Data Redundancy

 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

 The flexibility is offered through the concept of Polymorphism.


 A scenario can be considered for a better understanding of the concept.
 A person can behave differently whenever the surroundings change.
 For example, if the person is in a market, the person will behave like a
customer, or the behavior might get changed to a student when the person is
in a school or any institution.
6. Solving problems

 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

 The C++ identifier is a name used to identify a variable, function, class,


module, or any other user-defined item.
 An identifier starts with a letter A to Z or a to z or an underscore (_) followed
by zero or more letters, underscores, and digits (0 to 9).
 C++ does not allow punctuation characters such as @, $, and % within
identifiers.
 C++ is a case-sensitive programming language.
 Thus, Manpower and manpower are two different identifiers in C++.
Variable

 Variables in C++ is a name given to a memory location.


 It is the basic unit of storage in a program.
 The value stored in a variable can be changed during program execution.
 A variable is only a name given to a memory location, all the operations done
on the variable effects that memory location.
Constant operators

 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

 An expression in C++ is an order collection of operators and operands which


specifies a computation.
 An expression can contain zero or more operators and one or more operands,
operands can be constants or variables.
 In addition, an expression can contain function calls as well which return
constant values.
 Examples of C++ expression:
 (a+b) - c
 (x/y) -z
 4a2 - 5b +c
 (a+b) * (x+y)
String

 Strings are used for storing text.


 A string variable contains a collection of characters surrounded by double
quotes.
 Example
 Create a variable of type string and assign it a value:
 string greeting = "Hello“;
 Example
 #include <iostream>
 #include <string>
 using namespace std;
Cont….

 int main() {
 string greeting = "Hello";
 cout << greeting;
 return 0;
 }
 THANK YOU 

You might also like