You are on page 1of 9

OOP Using C ++ Lab File

BTCS-309

INTRODUCTION TO OBJECT ORIENTED PROGRAMMING


USING C++

The invention of OOP approach is to remove some of flaws encountered in POP.


OOP treats the data as critical element in the program development & does not allow
operate on it and protects it from accidently modification from outside function.
OOPallows decomposition of a program into a number of entities called objects & then
built data and functions around these objects.

FEATURES OF OOP :-
 Emphasis is on data rather than the procedure.
 Programs are divided into that are known as objects.
 Data structure are designed such that they characterise the object.
 Functions that operate on the data of an object are tired together in structure.
 Data is hidden & cannot be accessed by external function.
 Objects may communicate each other through functions.
 New data or function can be easily added whenever necessary.

Nikki Kaur Roll No. 1635003


OOP Using C ++ Lab File
BTCS-309

BASIC CONCEPTS USED IN THE OOP :-

1. OBJECT:-Any person, place or thing which has an independent existence is


called an object i.e the objects are basic, run time entities.
2. CLASS:-Collection of those objects which share the same attributes.
3. DATA ABSTRACTION:-It refers to the act of representing the features
without including background details on explanation. Abstraction supports data
hiding so that only relevant information is exposed to the user& rest of the
information remains hidden from the user.
4. ENCAPSULATION:-It refers to wrapping up of data and functions into a
single unit.
5. POLYMORPHISM:-Poly means many and morphism means forms. It deals
with different type of objects as if they are all of same type. It can be
implemented in c++ by three ways.
 Function overloading.
 Operator overloading
 Virtual functions.
6. INHERITANCE:-It is the capability of one class to inherit properties from
another class.
7. MESSAGE PASSING:-An object oriented program consists of a set of
objects that communicate with each other.
8. DYNAMIC BINDING:-It refers to linking of procedural call to the code that
will be executed only at run-time.

Nikki Kaur Roll No. 1635003


OOP Using C ++ Lab File
BTCS-309

ADVANTAGES OF OOP:-

 OOP provides a clear modular structure for programs.


 It is good for defining abstract data types.
 Implementation details are hidden from other modules and other modules has a clearly
defined interface.
 It is easy to maintain and modify existing code as new objects can be created with small
differences to existing ones.
 objects, methods, instance, message passing, inheritance are some important properties
provided by these particular languages
 encapsulation, polymorphism, abstraction are also counts in these fundamentals of
programming language.
 It implements real life scenario.
 In OOP, programmer not only defines data types but also deals with operations applied
with data structures.

DIFFERENCE BETWEEN C & C++

C C++
C was developed by Dennis C++ was developed by Bjarne Stroustrup in 1979
Ritchie between 1969 and 1973 at with C++'s predecessor "C with Classes".
AT&T Bell Labs.

When compared to C++, C is a C++ is a superset of C. C++ can run most of C


subset of C++. code while C cannot run C++ code.

C supports procedural C++ supports both procedural and object oriented


programming paradigm for code programming paradigms; therefore C++ is also
development. called a hybrid language.

C does not support object oriented Being an object oriented programming language
programming; therefore it has no C++ supports polymorphism, encapsulation, and
support for polymorphism, inheritance.
encapsulation, and inheritance.

In C (because it is a procedural In C++ (when it is used as object oriented


programming language), data and programming language), data and functions are
functions are separate and free encapsulated together in form of an object. For
entities. creating objects class provides a blueprint of
structure of the object.

Nikki Kaur Roll No. 1635003


OOP Using C ++ Lab File
BTCS-309

In C, data are free entities and can be In C++, Encapsulation hides the data to
manipulated by outside code. This is ensure that data structures and operators
because C does not support information are used as intended.
hiding.

C, being a procedural programming, it is a While, C++, being an object oriented


function driven language. programming, it is an object driven
language.

C does not support function and operator C++ supports both function and operator
overloading. overloading.

C does not allow functions to be defined In C++, functions can be used inside a
inside structures. structure.

C does not have namespace feature. C++ uses NAMESPACE which avoid
name collisions.
A namespace is a declarative region that
provides a scope to the identifiers (the
names of types, functions, variables, etc)
inside it. Namespaces are used to
organize code into logical groups and to
prevent name collisions that can occur
especially when your code base includes
multiple libraries. All identifiers at
namespace scope are visible to one
another without qualification. Identifiers
outside the namespace can access the
members by using the fully qualified
name for each identifier.
C uses functions for input/output. For C++ uses objects for input output. For
example scanf and printf. example cin and cout.
C does not support reference variables. C++ supports reference variables.

C has no support for virtual and friend C++ supports virtual and friend
functions. functions.
C provides malloc() and calloc()functions C++ provides new operator for memory
for dynamic memory allocation, allocation and delete operator for
and free() for memory de-allocation. memory de-allocation.
C does not provide direct support for error C++ provides support for exception

Nikki Kaur Roll No. 1635003


OOP Using C ++ Lab File
BTCS-309

handling (also called exception handling) handling. Exceptions are used for "hard"
errors that make the code incorrect.

Practical 1
Aim:-To show use of 'const' variable.
#include <iostream.h >

#include <conio.h >


void main ()
{
clrscr();
const int x=6;
x=x+1;
cout<<''\n modified value of x =”<<endl ;
cout<<x;
getch ();
}

Output :-

Nikki Kaur Roll No. 1635003


OOP Using C ++ Lab File
BTCS-309

Practical 2
Aim:- To show the use of #define directive.
#include<iostream.h>
#include<conio.h>
#define p 3.142
void main()
{
clrscr();
long intr,a,c;
cout<<"\n enter the value of r=";
cin>>r;
a=p*r*r;
cout<<"\n area is="<<int(a)<<endl;
c=2*p*r;
cout<<"\n circumference="<<int(c)<<endl;
getch();
}

Output :-

Nikki Kaur Roll No. 1635003


OOP Using C ++ Lab File
BTCS-309

Practical 3
Aim:-To show the use of size of operator.
#include <iostream.h >
#include <conio.h >
void main ()
{
clrscr();
float a;
cout<<''size of int =''<<size of (int)<<endl;
cout<<''size of float =''<<size of (a)<<endl ;
cout<<''size of char=''<<endl ;
double b;
cout <<''size of double =''<< size of (b)<<endl ;
cout <<'' size of long double =''<<size of (long double) <<endl ;
getch ();
}

Output :-

Nikki Kaur Roll No. 1635003


OOP Using C ++ Lab File
BTCS-309

Nikki Kaur Roll No. 1635003


OOP Using C ++ Lab File
BTCS-309

Practical 4
Aim :-Program to show the use of conditional operator.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
inta,b,c,big;
cout<<"\n enter three values"<<endl;
cin>>a>>b>>c;
big=(a>b)?(a>c?a:c):(b>c?b:c);
cout<<"largest no="<<big;
getch();
}

Output :-

Nikki Kaur Roll No. 1635003

You might also like