Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
52 views

Object Oriented Programming Language Using C++

C++ is an object-oriented programming language that uses classes and objects. It supports concepts like encapsulation, inheritance, polymorphism, and abstraction. C++ was developed from C and supports both procedural and object-oriented programming. Some key features include classes, objects, inheritance, polymorphism, and templates. C++ is widely used for system applications like operating systems and databases due to its speed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Object Oriented Programming Language Using C++

C++ is an object-oriented programming language that uses classes and objects. It supports concepts like encapsulation, inheritance, polymorphism, and abstraction. C++ was developed from C and supports both procedural and object-oriented programming. Some key features include classes, objects, inheritance, polymorphism, and templates. C++ is widely used for system applications like operating systems and databases due to its speed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

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.

Why we need object-oriented programming language:


1. To make the development and maintenance of project more efficient & effortless.
2. To reduce the complexity of lengthy code in the single program.
3. To provide the feature of data hiding that is good for give security to program.
4. To solve the real-world problems easily in the oops
5. It provides code reusability

4 main principles of oops:

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

2. c used #include<stdio.h> header file 2. c++ used #include<iostream> header file

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

5. namespace feature is not present in c 5. in c++ namespace feature is present

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:

#include<iostream> // it is preprocessor directive

using namespace std;

int main ()
{

int num1, num2;

cout<<"Enter Two Numbers: "endl; /* enter the value for num1 and num2

cin>>num1>>num2; and display it on screen */

cout<<”number 1=”<<num1<<”number 2=”<<num2 <<endl; // cascading

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 \* ..*/
/*………………………….
……………………………*/

Int main() function:-


int main() is the main function of program it specifies the integer type of value.
In our program int main() function needs to return some integer at the end of the execution , so
we returning 0 at the end of the program. 0 is the standard for the successful execution of the
program otherwise error might occure

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.

Using namespace std :


Using namespace std;
Namespace in C++ is the declarative part where the scope of identifiers like functions, the name
of types, classes, variables, etc., are declared. The code generally has multiple libraries, and the
namespace helps in avoiding the ambiguity that may occur when two identifiers have the same
name.
Std is a standard namespace it support cout and cin
For example, suppose you have two functions named calculate(), and both are performing
different tasks. One calculate() function is doing the multiplication, and another is doing the
addition. So in this case, to avoid ambiguity, you will declare both the functions in two different
namespaces. These namespaces will differentiate both the functions and also provide
information regarding both the functions

Input output operation:-


In C++ cout<< is used for print or display the content of the messages (statements) and values
to the output screen(console).it is same like printf() function in C
<< this is insertion operator
Eg: cout<<”this is book”;
In C++ Cin >> is used for take the values from the output screen (console) it take the data from
user.it is same like scanf() function in C.
>> this is extraction operator
Eg: cin>> a;

Cascading of I/O operations:


The multiple use of “ << ” insertion operator and “ >> ” extraction operator in the single line
statement is called cascading
Eg: cout<<”the value of a&b is :”<<a<<b<<endl;
Eg:cin>>a>>b;

Declaring local variables:


int main()
{ // here declaration of variables

int i; // integer type value


float a; // floating type value
string name; // string
char str[10];
double d; // double type value
cout<< “enter value for i,a,name,str,d:”;
cin>>i>>a>>name>>str>>d;
cout<<”values for i,a,name,str,d is=”<<i<<a<<str<<name<<d;
return 0;
}

The bool Data Type :


C++ defines a built-in Boolean type called bool. type bool can store only the values true or
false, which are keywords defined by C++. As explained in Part One, automatic conversions
take place which allow bool values to be converted to integers, and vice versa. Specifically,
any non-zero value is converted to true and zero is converted to false. The reverse also
occurs; true is converted to 1 and false is converted to zero. Thus, the fundamental concept of
zero being false and non-zero being true is still fully entrenched in the C++ language

Old-Style vs. Modern C++:

/* /*
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>

The C++ Keywords:


There are 63 keywords currently defined for Standard C++. early versions of C++ defined
the overload keyword, C++ is a case-sensitive language and it requires that all keywords be
in lowercase.
Keywords are :
asm auto bool break case catch char class const const _cast
continue default delete do double dynamic_cast else enum explicit
export extern false float for friend goto if inline int long mutable
namespace new operator private protected public register reinterpret_cast
return short signed sizeof static static_cast struct switch template this throw
true try typedef typeid typename union unsigned using virtual void volatile
wchar_t while

Some C++ example programs :


1.C++ program to Check if a number is even or odd.
#include <iostream>
using namespace std;
int main() {
int a ;
cin>>a;
if(a%2 == 0) // if remainder is zero then even number
cout<<”even”;
else
cout<<”odd”;
return 0;
}

2.Write a program to swap two numbers in C++


#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 20;
cout<<a<<" "<<b<<endl;
int temp = a;
a = b;
b = temp;
cout<<a<<" "<<b<<endl;
return 0;
}

3.Write a program in C++ to check whether a number is prime or not.


#include <iostream>
using namespace std;
int main() {
int a ;
cin>>a;
int b = 2;
//start from b as 1 can divide any number
bool prime = true;
while(b!=a){
if(a%b == 0)
{
prime = false;
break;
}
b++;
}
if(prime)
cout<<"prime";
else
cout<<"not prime";
return 0;
}

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;
}

5.Write a program to find the GCD of two numbers in C++.


#include <iostream>
using namespace std;
int gcd(int a,int b){
if(b == 0)
return a;
return gcd(b,a%b);
}
int main() {
int a ,b ;
cin>>a>>b;
cout<<gcd(a,b);
return 0;
}

6.Write a function to find the length of a string in C++.


#include <iostream>
using namespace std;
int main()
{
string str;
cin>>str;
int count = 0;
for(int i = 0;str[i];i++) // till the string character is null
count++;
cout<<count;
}

7.Write a program to find the reverse of a number


#include <iostream>
using namespace std;
int main() {
int a = 1223;
int res = 0;
while(a!=0){
int dig = a%10;
res = res*10 + dig;
a =a/10;
}
cout<<res;
return 0;
}
8.C++ to check whether a number is a palindrome or not
#include <iostream>
using namespace std;
int reverse(int a){
int res = 0;
while(a!=0){
int dig = a%10;
res = res*10 + dig;
a =a/10;
}
return res;
}
int main() {
int a = 1221;
int b = reverse(a);
if(a == b)
cout<<"Palindrome";
else
cout<<"Not palindrome";
return 0;
}

9.C++ program to Find the factorial of a number.


#include <iostream>
using namespace std;
int factorial(int n){
int res = 1;
for(int i = 2;i<=n;i++)
res = res*i;
return res;
}
int main() {
int a = 5;
cout<<factorial(a)<<endl;
return 0;
}

10.C++ program to sort the given array in ascending order.


#include <iostream>
using namespace std;
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void bubblesort(int arr[], int n)
{
for (int i = 0; i < n-1; i++)
for (int j = 0; j arr[j+1])
swap(&arr[j], &arr[j+1]);

for ( int i = 0; i < n; i++)


cout << arr[i] << " ";
cout << endl;
}
int main() {
int arr[5] = {4,3,5,1,2};
bubblesort(arr,5);
return 0;
}

You might also like