0% found this document useful (0 votes)
6 views17 pages

Oops

The document explains various concepts in C++ including storage classes, program structure, and differences between C and C++. It covers object-oriented programming principles such as classes, objects, encapsulation, abstraction, polymorphism, and inheritance, detailing their definitions and types. Additionally, it provides examples of compile-time and runtime polymorphism, as well as different inheritance types in C++.

Uploaded by

nagarajlaxmi756
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views17 pages

Oops

The document explains various concepts in C++ including storage classes, program structure, and differences between C and C++. It covers object-oriented programming principles such as classes, objects, encapsulation, abstraction, polymorphism, and inheritance, detailing their definitions and types. Additionally, it provides examples of compile-time and runtime polymorphism, as well as different inheritance types in C++.

Uploaded by

nagarajlaxmi756
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

UNIT -5

1.EXPALINE Storage Classes IN C++


Storage Classes are used to describe the features of a variable/function. These
features basically include the scope, visibility and life-time which help us to trace the
existence of a particular variable during the runtime of a program. To specify the
storage class for a variable, the following syntax is to be followed:
Syntax:
storage_class var_data_type var_name;
C++ uses 5 storage classes, namely:
1. auto
2. register
3. extern
4. static
5. mutable

Automatic Storage Class It is the default storage class for all local
variables. The auto keyword is applied to all local variables automatically.
The auto keyword provides type inference capabilities

1. {
2. auto int y;
3. float y = 3.45;
4. }

The above example defines two variables with a same storage class, auto can only be used
within functions.
External Storage Class
The extern variable is visible to all the programs. It is used if two or more files are sharing same
variable or function

Static Storage Class


The static variable is initialized only once and exists till the end of a program. It retains its value
between multiple functions call.

The static variable has the default value 0 which is provided by compiler.

1. #include <iostream>
2. using namespace std;
3. void func() {
4. static int i=0; //static variable
5. int j=0; //local variable

Register Storage Class


The register variable allocates memory in register than RAM. Its size is same of register size. It
has a faster access than other variables.

It is recommended to use register variable only for quick access such as in counter.

Note: We can't get the address of register variable.

1. register int counter=0;


2.EXPLAIN STRUCTURE OF C++

The C++ program is written using a specific template structure. The structure of the
program written in C++ language is as follows:

Documentation Section:
 This section comes first and is used to document the logic of the program that the
programmer going to code.
 It can be also used to write for purpose of the program.
 Whatever written in the documentation section is the comment and is not compiled
by the compiler.
 Documentation Section is optional since the program can execute without them.
Below is the snippet of the same:
Header Files:
 Generally, a program includes various programming elements like built-in functions,
classes, keywords, constants, operators, etc. that are already defined in the
standard C++ library.
When the compiler processes the instruction #include<iostream>, it includes

Namespaces:
 A namespace permits grouping of various entities like classes, objects, functions,
and various C++ tokens, etc. under a single name.
 Any user can create separate namespaces of its own and can use them in any other
program.
 .
Definition Section:
 It is used to declare some constants and assign them some value.
 In #define is a compiler directive which tells the compiler whenever the message is
found replace it with “Factorial\n” .
 .
Global Declaration Section:
 Here the variables and the class definitions which are going to be used in the
program are declared to make them global.
 The scope of the variable declared in this section lasts until the entire program
terminates.
 .
Function Declaration Section:
 It contains all the functions which our main functions need.
 Usually, this section contains the User-defined functions.
 This part of the program can be written after the main function but for this, write the
function prototype in this section for the function which for you are going to write
code after the MAIN FUNCTION
 main function.
The main function tells the compiler where to start the execution of the program

all the instructions which are written in the curly braces {

3. C VS C++

C C++

C was developed by Dennis Ritchie between the C++ was developed by Bjarne Stroustrup in
year 1969 and 1973 at AT&T Bell Labs. 1979.

C does no support polymorphism, encapsulation, C++ supports polymorphism, encapsulation,


and inheritance which means that C does not and inheritance because it is an object oriented
support object oriented programming. programming language.

C is a subset of C++. C++ is a superset of C.

C contains 32 keywords. C++ contains 63 keywords.

C++ is known as hybrid language because C++


For the development of code, C supports both procedural and object oriented
supports procedural programming. programming paradigms.

Data and functions are separated in C because it is Data and functions are encapsulated together in
a procedural programming language. form of an object in C++.

Data is hidden by the Encapsulation to ensure


that data structures and operators are used as
C does not support information hiding. intended.

Built-in & user-defined data types is supported


Built-in data types is supported in C. in C++.

C is a function driven language because C is a C++ is an object driven language because it is


C C++

procedural programming language. an object oriented programming.

Function and operator overloading is not Function and operator overloading is supported
supported in C. by C++.

C is a function-driven language. C++ is an object-driven language

Functions in C are not defined inside structures. Functions can be used inside a structure in C++.

Namespace is used by C++, which avoid name


Namespace features are not present inside the C. collisions.

Header file used by C is stdio.h. Header file used by C++ is iostream.h.

Reference variables are not supported by C. Reference variables are supported by C++.

Virtual and friend functions are not supported by Virtual and friend functions are supported by
C. C++.

C does not support inheritance. C++ supports inheritance.

Instead of focusing on data, C focuses on method C++ focuses on data instead of focusing on
or process. method or procedure.

C provides malloc() and calloc() functions C++ provides new operator for memory
for dynamic memory allocation, and free() for allocation and delete operator for memory de-
memory de-allocation. allocation.

Direct support for exception handling is not


supported by C. Exception handling is supported by C++.

scanf() and printf() functions are used for


input/output in C. cin and cout are used for input/output in C++.

C structures don’t have access modifiers. C ++ structures have access modifiers.


4.Object Oriented Programming in C++
 Difficulty Level : Easy
 Last Updated : 28 Apr, 2020
TABLE OF CONTENT:
1. Introduction
2. Class
3. Objects
4. Encapsulation
5. Abstraction
6. Polymorphism
7. Inheritance
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 main aim
of OOP is to bind together the data and the functions that operate on them so
that no other part of the code can access this data except that function.
Characteristics of an Object Oriented Programming language

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 brand but all of them will share some common properties like all of them
will have 4 wheels, Speed Limit, Mileage range etc. So here, Car is the class and
wheels, speed limits, mileage are their properties.
 A Class is a user-defined data-type which has data members and member
functions.
 Data members are the data variables and member functions are the functions used
to manipulate these variables and together these data members and member
functions define the properties and behaviour of the objects in a Class.
 In the above example of class Car, the data member will be speed limit, mileage etc
and member functions can apply brakes, increase speed etc.

Object: (OBJECT IS PHYSICAL THINGS )An Object is an identifiable entity with some
PROPERTIES and METHODS. 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.

Encapsulation: In normal terms, Encapsulation is defined as wrapping up of data and


information under a single unit. In Object-Oriented Programming, Encapsulation is
defined as binding together the data and the functions that manipulate them.

Abstraction: Data abstraction is one of the most essential and important features of
object-oriented programming in C++. Abstraction means displaying only essential
information and hiding the details. Data abstraction refers to providing only essential
information about the data to the outside world, hiding the background details or
implementation.

 Abstraction using Classes: We can implement Abstraction in C++ using classes.


The class helps us to group data members and member functions using available
access specifiers. A Class can decide which data member will be visible to the
outside world and which is not.
 Abstraction in Header files: One more type of abstraction in C++ can be header
files. For example, consider the pow() method present in math.h header file.
Whenever we need to calculate the power of a number, we simply call the function
pow() present in the math.h header file and pass the numbers as arguments without
knowing the underlying algorithm according to which the function is actually
calculating the power of numbers.

Polymorphism: The word polymorphism means having many forms. In simple words,
we can define polymorphism as the ability of a message to be displayed in more than
one form.
A person at the same time can have different characteristic. Like a man at the same
time is a father, a husband, an employee. So the same person posses different
behaviour in different situations. This is called polymorphism.
An operation may exhibit different behaviours in different instances. The behaviour
depends upon the types of data used in the operation.
 Operator Overloading: The process of making an operator to exhibit different
behaviours in different instances is known as operator overloading.
 Function Overloading: Function overloading is using a single function name to
perform different types of tasks.
Polymorphism is extensively used in implementing inheritance.
Inheritance: The capability of a class to derive properties and characteristics from
another class is called Inheritance. Inheritance is one of the most important features of
Object-Oriented Programming.
 Sub Class: The class that inherits properties from another class is called Sub class
or Derived Class.
 Super Class:The class whose properties are inherited by sub class is called Base
Class or Super class.
 Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to
create a new class and there is already a class that includes some of the code that
we want, we can derive our new class from the existing class. By doing this, we are
reusing the fields and methods of the existing class.

5.EXPLAINE POLYMORPHISM AND TYPES IN CPP?

Polymorphism: The word polymorphism means having many forms. In simple words,
we can define polymorphism as the ability of a message to be displayed in more than
one form.
A person at the same time can have different characteristic. Like a man at the same
time is a father, a husband, an employee. So the same person posses different
behaviour in different situations. This is called polymorphism.

.
The word “polymorphism” means having many forms.

Types of Polymorphism
 Compile-time Polymorphism
 Runtime Polymorphism
1. Compile-Time Polymorphism
This type of polymorphism is achieved by function overloading or operator overloading.

A. Function Overloading

When there are multiple functions with the same name but different parameters, then
the functions are said to be overloaded, hence this is known as Function Overloading.
#include <iostream>
using namespace std;

class a
{
public:

void func(int x)
{
cout << "value of x is " << x << endl;
}

void func(double x)
{
cout << "value of x is " << x << endl;
}
void func(int x, int y)
{
cout << "value of x and y is " << x << ", "
<< y
<< endl;
}
};

int main()
{
a obj1;

obj1.func(7);

obj1.func(9.132);

obj1.func(85, 64);
return 0;
}
Output
value of x is 7
value of x is 9.132
value of x and y is 85, 64

B. Operator Overloading

C++ has the ability to provide the operators with a special meaning for a data type, this
ability is known as operator overloading. For example, we can make use of the addition
operator (+) for string class to concatenate two strings. We know that the task of this
operator is to add two operands. So a single operator „+‟, when placed between integer
operands, adds them and when placed between string operands, concatenates them.
#include <iostream>
using namespace std;

class Complex
{
private:
int real, imag;
public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}

Complex operator+(Complex const& obj)


{
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print()
{
cout << real << " + i" << imag << endl;
}
};

int main()
{
Complex c1(10, 5), c2(2, 4);

Complex c3 = c1 + c2;
c3.print();
}
6.EXPLAINE Inheritance AND TYPES IN CPP?

Inheritance
The capability of a class to derive properties and characteristics from another class is
called Inheritance. Inheritance is one of the most important features of Object-
Oriented Programming.

inheritance is a feature or a process in which, new classes are created from the
existing classes. The new class created is called “derived class” or “child class” and
the existing class is known as the “base class” or “parent class”. The derived class now
is said to be inherited from the base class.
 Sub Class: The class that inherits properties from another class is called Subclass
or Derived Class.
 Super Class: The class whose properties are inherited by a subclass is called Base
Class or Superclass.

#include <iostream.h>
using namespace std;

class Parent
{
public:
int iDP;
};

class Child : public Parent


{
public:
int idC;
};

int main()
{
Child obj1;

obj1.id_c = 7;
obj1.id_p = 91;
cout << "Child id is: " << obj1.id_c << '\n';
cout << "Parent id is: " << obj1.id_p << '\n';

return 0;
}
Types Of

Inheritance:-

1. Single inheritance
2. Multilevel inheritance
3. Multiple inheritance
4. Hierarchical inheritance
5. Hybrid inheritan

Types of Inheritance in C++

1. Single Inheritance: In single inheritance, a class is allowed to inherit from only one
class. i.e. one subclass is inherited by one base class only.

Syntax:
class subclass_name : access_mode base_class
{
// body of subclass
};

OR

class A
{
... .. ...
};

class B: public A
{
... .. ...
};

2. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can


inherit from more than one class. i.e one subclass is inherited from more than
one base class.

Syntax:
class subclass_name : access_mode base_class1, access_mode base_class2, ....
{
// body of subclass
};

class B
{
... .. ...
};
class C
{
... .. ...
};
class A: public B, public C
{
... ... ...
};

3. Multilevel Inheritance: In this type of inheritance, a derived class is created from


another derived class.

Syntax:-
class C
{
... .. ...
};
class B:public C
{
... .. ...
};
class A: public B
{
... ... ...
};

4. Hierarchical Inheritance: In this type of inheritance, more than one subclass is


inherited from a single base class. i.e. more than one derived class is created from a
single base class.

Syntax:-
class A
{
// body of the class A.
}
class B : public A
{
// body of class B.
}
class C : public A
{
// body of class C.
}
class D : public A
{
// body of class D.
}

5. Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by combining more


than one type of inheritance. For example: Combining Hierarchical inheritance and
Multiple Inheritance.
Below image shows the combination of hierarchical and multiple inheritances:

You might also like