01 Oop
01 Oop
01 Oop
• C++ is the most used and most popular programming language developed by Bjarne Stroustrup at
AT & T Bell in 1980’s.
• C++ is the combination of simula67 and C languages.
• Initially it is called as the C with classes and later it is renamed as C++.
• C++ is a high-level and object-oriented programming language. This language allows developers
to write clean and efficient code for large applications and software development, game
development, and operating system programming.
• It is an expansion of the C programming language to include Object Oriented Programming(OOPs)
and is used to develop programs for computers.
Basic Object-Oriented Programming (OOPS) Concept in C++
• There are some basic concepts that act as the building blocks of OOPs.
• Classes & Objects
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism
• Class and objects :
• An Object can be defined as an entity that has a state and behavior, or in other words, anything
that exists physically in the world is called an object. It can represent a dog, a car, a person, a
table, etc. An object means a combination of data and programs, which further represent an
entity.
• Class: It is a collection of object.
• Class is a structure where we can defines variables and methods to utilize by objects.
• Class can be defined as a blueprint of the object. It is basically a collection of objects which
act as building blocks.
• 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.
• More than one function with the same name but different working.
Types of polymorphism:
1. Compile time polymorphism : This type of polymorphism is achieved by function overloading or operator
overloading. Memory will be allocated at compile time.
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. Functions can be overloaded by changing the
number of arguments or/and changing the type of arguments. In simple terms, it is a feature of object-
oriented programming providing many functions that have the same name but distinct parameters when
numerous tasks are listed under one function name.
B. Operator Overloading: It is a specific case of polymorphism, where different operators have different
implementations depending on their arguments.
2. Runtime Polymorphism
This type of polymorphism is achieved by Function Overriding. Late binding and dynamic polymorphism are
other names for runtime polymorphism. The function call is resolved at runtime in runtime polymorphism.
Overriding is a run time polymorphism where more than one method is having the same name, number of
parameters and the type of the parameters.
• Virtual function : A virtual function is a member function that is declared in the base class using the
keyword virtual and is re-defined (Overridden) in the derived class. It tells the compiler to perform late
binding where the compiler matches the object with the right called function and executes it during the
runtime. This technique falls under Runtime Polymorphism.
• Inheritance : It is mechanism of deriving a new class from an existing
class. Five types of inheritance are:
1. Single level
2. Multi level
3. Multiple
4. Hierarchical
5. Hybrid
• Single level : In single inheritance, a class is allowed to inherit from only one class. i.e. one
subclass is inherited by one base class only.
• Multi level inheritance:
• In this type of inheritance, a derived class is created from another derived class.
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.
• 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.
•
• Hybrid inheritance: Hybrid Inheritance is implemented by combining more than one type of
inheritance.
Basic structures of a C++ Program
Documentation Section
Preprocessor directives
Class declaration
Main function
1. Document section:
• It can be also called as comment section.
• 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.
• C++ supports two comment styles: single line comment and multiline comment. Single line comments are
used to define line-by-line descriptions. Double slash (//) is used to represent single line comments.
• / / single line comment
• Multiline comments are used to define multiple lines descriptions and are represented as / * * /. For example,
consider this statement.
• /* An example to demonstrate multiline comment */
• Preprocessor directives
• The statement which are executed before compilation are known as preprocessor
directives. There are two types of preprocessor directives.
1. Header file: collection of library function
2. Macro : mainly used to defined symbolic constant
• Class declaration :
• Class is collection of object.
• Class mainly contain variables and function.
• Variable which is define inside a class is called as the member variable and function which are define inside a
class is called as the member function.
• So class is a collection of member variable and member function.
class classname
{
member variable
member function
};
• Member function definition:
• Here we can define member function in two ways, inside the class or outside the class.
• If a member function is defined inside a class then there is no need of function declaration while if a member
function is defined outside the class then the corresponding function should declare inside a class.
• Main function:
• Like C program, C++ is also a collection function.
• Any C++ program must contain at least one function that is the main function.
• The execution of any C++ program must start from the main function and the entire program will
be executed.
C++ std Namespace
• In C++, a namespace is a collection of related names or identifiers (functions, class, variables)
which helps to separate these identifiers from similar identifiers in other namespaces or the global
namespace.
• The identifiers of the C++ standard library are defined in a namespace called std.
• In order to use any identifier belonging to the standard library, we need to specify that it belongs to
the std namespace. One way to do this is by using the scope resolution operator ::. For example,
float 4bytes
double 8bytes
long double 12bytes
wchar_t 2 or 4 bytes 1 wide character
• The size of variables might be different from those shown in the above table, depending on the compiler and
the computer you are using.
• Following is the example, which will produce correct size of various data types on your computer.
Enumerated Data Type
• Enumerated Types :
• An enumeration is a user-defined data type that consists of integral constants.
• Enum is a collection of named integer constant means it’s each element is assigned by integer value.
• To define an enumeration, keyword enum is used.
• The general form of an enumeration type is −
• enum enum-name { list of names } var-list;
• Here, the enum-name is the enumeration's type name. The list of names is comma separated.
• For example, the following code defines an enumeration of colors called colors and the variable c of type color. Finally, c is
assigned the value "blue".
• enum color { red, green, blue } c;
• c = blue;
• By default, the value of the first name is 0, the second name has the value 1, and the third has the value 2, and so on. But
you can give a name, a specific value by adding an initializer.
• enum season { spring, summer, autumn, winter };
• Here, the name of the enumeration is season.
• And, spring, summer and winter are values of type season.
• By default, spring is 0, summer is 1 and so on. You can change the default value of an enum element during declaration (if
necessary).
enum season
{ spring = 0,
summer = 4,
autumn = 8,
winter = 12
};
Exercise
Exercise
Contd.