Oops
Oops
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
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
It is recommended to use register variable only for quick access such as in counter.
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
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.
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++.
Function and operator overloading is not Function and operator overloading is supported
supported in C. by C++.
Functions in C are not defined inside structures. Functions can be used inside a structure in C++.
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++.
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.
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.
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.
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.
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;
}
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;
};
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
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
{
... .. ...
};
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
{
... ... ...
};
Syntax:-
class C
{
... .. ...
};
class B:public C
{
... .. ...
};
class A: public B
{
... ... ...
};
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.
}