Object Oriented Programming Language Using C++
Object Oriented Programming Language Using C++
Module 1:
Object oriented programming language: -
In general object means it is real world entity and oriented means interested to a particular
kind of thing or entity
“An object-oriented programming language is a programming paradigm based on the concept
of object, contains data and code, data in the form of fields (known as attributes or properties)
and code in the form of procedure (known as methods or function) is called object-oriented
programming language “
An object is a instance (known as process of creating object) of the class, all the members of
the class can be accessed through object.
Object is used to allocating memory space for the classes, variables and functions etc.
1.Encapsulation:
Encapsulation is the mechanism of binding the data and also grouping the data members and
member functions into a single unit (i.e., inside the class).
encapsulation is used to keep the data and function safely from other interference or
missuses.
Example 1: consider the capsule there are different compositions are grouped to make a
complete medicine for fever problem the grouping of that compositions into a single unit this
process is encapsulation
Example 2: ATM is a class. It takes data from the user (money amount and PIN) and displays
data as icons and options. It processes the request(functions). So, it contains both data and
functions wrapped/integrated under a single ATM. this process is encapsulation.
2.Polymorphism:
the word poly means – “many “and morph means – “forms”
Polymorphism means it is ability to take more than one form, it acts as different behavior in
different situations.
In c++ a function can be many forms i.e sometimes using for overloading, overriding, virtual
and inline etc. at different situation
Example: Consider a person, who can have multiple characteristics at a time, the person can
be a father, a son, and an employee at the same time. The same person acts differently in
different scenarios, this shows polymorphism.
3.Inheritance:
Inheritance is the process of acquiring the properties of one class to another class i.e the base
class data members and member functions properties access to derive class is called inheritance
It builds a relationship between base class and derived class
Example: The real-life example of inheritance is child and parents, all the properties of father
are inherited by his son
4.Data abstraction:
Data abstraction is the process of hiding the details of all the code inside the class,it provides
only particular part of code to the outside class for when it is important is called data
abstraction
Example: Consider a real-life example of a man driving a car. The man only knows that
pressing the accelerator will increase the speed of the car or applying brakes will stop the car
but he does not know how on pressing the accelerator the speed is actually increasing, he does
not know about the inner mechanism of the car or the implementation of the accelerator, brakes,
etc in the car. This is data abstraction
Overview of C++:
What is C ++ ?
“C++ is a general purpose, case-sensitive, free-form programming language that supports
object-oriented, procedural and generic programming language.”
C++ is the augmented(incremented) version of c.
C++ was developed from c language c++ early called “ c with classes”.
The idea of c++ comes from the c increment operator “++”.
C++ is a hybrid language why because of it support both procedure and object oriented
programming language.
C++ was invented by Bjarne Stroustrup in the year of 1979 at Bell laboratory .
Difference between c and c++:-
C C++
1. c support only procedural oriented 1. c++ support both procedure and object-
programming language oriented programming language
3. c using printf () and scanf () for I/O 3. c++ using cout<<, cin>> for I/O operation
operation
4. c using .c is the file extension 4. c++ using .cpp is the file extension
6. c support only built-in data type 6. c++ support both built-in and user defined
datatype
Advantages of C++:
1. It support object oriented programming language main focus on object.
2. It reduce the compilation and execution time to make program run faster.
3. It is platform independent.
4. It contains many built-in library function help to write program code quickly.
5. It is closer to hardware .
6. It very useful where hardware and software are closer coupled together .
7. It support pointer.
8. It is capable of running on a small scale as well as a large scale of data.
9. C++ provides the feature of portability allowing us to develop codes without caring
about the hardware.
10. C++ supports Dynamic Memory Allocation,which helps to free and allocate memory.
Since there is no garbage collection, C++ gives the programmer total control over
memory management.
Applications of C++:
1. C++ was used in developing most of the operating systems in the world.
2. C++ was also used in developing most of the GUI (Graphical User Interface)
applications that we use regularly.
3. C++ has faster execution, it helps in developing rendering engines in browsers.
4. C++ is used in 3D animation, image processing, and real-time simulation applications.
5. C++ is also used in developing the most important DBMS engines like MySQL,
Postgres, Redis, and Oracle.
6. C++ is closer to the hardware, its speed and faster execution help in game development
system.
7. It is used in banking application.
Simple C++ program:
int main ()
{
cout<<"Enter Two Numbers: "endl; /* enter the value for num1 and num2
return 0;
Comments :
A comment is basically a text note that gives an explanation about the source code. comments
increase the readability of the program. To disable lines of code in a program by surrounding
them with comment-start and comment-stop characters.
2 types of comments:
1.Single line comments:
It represent the double slash( // )
// …………………………..
2.Multiline comments :
It represent the \* ..*/
/*………………………….
……………………………*/
IOstreams:
#include<iostream>
iostream stands for standard input-output stream.
#include iostream provides the most used standard input and output streams, cin and cout.
#include iostream declares objects that control reading from and writing to the standard
streams. In other words, the iostream library is an object-oriented library that provides input
and output functionality using streams.
/* /*
An old-style C++ program. A modern-style C++ program that uses
*/ the new-style headers and a namespace.
#include <iostream.h> */
int main() #include <iostream>
{ using namespace std;
return 0; int main()
} {
return 0;
}
the new-style headers supported by Standard C++.
<iostream> <fstream> <vector> <string>
4.Write a C++ program to Compute the power a given number to a given power.
#include <iostream>
using namespace std;
int main()
{
int power;
float base, result = 1;
cin >> base >>power;
while (power != 0) {
result *= base;
power--;
}
cout << result;
return 0;
}