0% found this document useful (0 votes)
116 views17 pages

Geetanjali Dalvi

Object oriented programming uses concepts like classes, objects, inheritance, polymorphism, and encapsulation. C++ is an object-oriented language that is an extension of C and adds object-oriented features. The document discusses basic concepts of OOP like classes, objects, encapsulation, inheritance, and polymorphism. It then covers beginning concepts in C++ like input/output operators, header files, variables, data types, functions, and memory management using new and delete operators.

Uploaded by

Preeti Paryani
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views17 pages

Geetanjali Dalvi

Object oriented programming uses concepts like classes, objects, inheritance, polymorphism, and encapsulation. C++ is an object-oriented language that is an extension of C and adds object-oriented features. The document discusses basic concepts of OOP like classes, objects, encapsulation, inheritance, and polymorphism. It then covers beginning concepts in C++ like input/output operators, header files, variables, data types, functions, and memory management using new and delete operators.

Uploaded by

Preeti Paryani
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

OBJECT ORIENTED PROGRAMMING

Table of Contents
Basic concept of OOPS……………………………………………… 2

Beginning with C++ ........................................................................ 4


Constructors and Destructor………………………………………..10
Inheritance……………………………………………………………...15

1
Chapter 1
Basic concept of OOPs
(1) Class :-
If is an user defined data type it is a collection of data members & member
functions which operate on that data member once a class has been
declared, we can create number of objects.

(2) Object :-
Objects are non time quantities or variables. Object is variable of class. They
may represent person, place, bank, etc.
Programming problems are analyzed in terms of object. The objects are
chosen as real world objects interact by sending messages to one another.

(3) Data encapsulation :-


The wrapping up of data & function into single unit ie. Class is known as data
encapsulation. The data is not accessible to outside world & it is available to
only those function which are declare inside the same class.

(4)Data hiding:-
The data is access only by the function within the same class no external
function can access that data.

(5)Data abstraction:-
Abstraction refers to representing essential features without including
background details. Data abstraction is the process of defining data type often
called abstract data type together with the principle of data hiding.

(6) Inheritance:-
Furniture Parent Class

Price
Weight
Color
With

Chair Table Child Class


Inheritance is the process by which the object of one class acquire the properties of
another class. This means deriving a new class from existing class. This deriving a
new class from existing class. This new class well have additional features along
with the already existing class.

2
New class i.e. derived from the extension class is known as child class.& from which
it is derived is called parent class.
(7)Polymorphism –
Void draw c)

Draw (Circle) Draw (Square) Draw (Triangle)


It is an another important oops concept it means ability to take more than one form.
A single operator can be use from deferent purpose is known as operator over
loading.
Operator
+
Int char
+ +
Int int
= int = string
We have same name function with different types of agreement as explained
This is called function over modify
It plays an important role is allowing object having different interval structure to
share same external interface.

Benefits of oop
(1) Through inheritance we can eliminate redundant code extend use of existing
class.
(2) The principle of data hiding helps programmer to build secure programmes
that can not be shared by other parts of the programs.
(3) It is possible to have multiple instances of an object.
(4) Program is divided into number of objects.
(5) Software complexity can be maintained.
(6) Oops can be easily up graded from small to large systems.
(7) Message passing techniques are simpler.

3
Chapter 2
Beginning with C++
Introduction:-

C++ is oop’s language. It was developed by AT & T bell labs. In early 80’s
It is an extension of c c++ is a superset of c. There are few minor
differences between c& c++ . c++ adds more features like classes, function
overloading, operator over loading, inheritance etc. It allows programmer to
built large programs with clarity, ease of maintenance with efficiently of c
language.

Program features:-
(1) Comments- c++ introduce new symbol i.e. // double slash for comments
followed by line to be ignored. It is non executable statements.
// single line
// cout << “HI “;
(2) Input operator :-
cin >> variable;
Int a;
Cin >>a;
To read the data from standard, input device i.e. keyboard. C++ provide
cin which is predefine object for standard input device >> symbol
represents extraction or get from operator.
(3) Output operator :-
Cout << “text”; cout << “enter value
Cout << variable; cout << a;
Cout << “ text “ << variable; cout<<”same”<<total;
To display output on standard output device, c++ provide cout which is
predefined object represents standard output string in c++ the symbol in
c++ the symbol << is called insertion put to operation.
(4) Header file :- for c++ the header file is < iostream.h >
Structure of c++
Inclosed File

Class definition
Data member
Member function

Main Program

4
3 Tokens, Expression & Control Structure
 Keywords :-
More than & keywords are available in c++. Keywords are reserved
words & cannot be used as names of variables
.
 Basic datatypes in c++
Datatype

User defined built in derived

Structure Class float void integral array function

Union float double int char pointer

Symbolic constant :-
Const float pi = 3.14;
Constant variable is a variable whose value cannot be changed. C++
require a constant to be initialized.
Declaration & variable:-
Anywhere in the program you can define the variable before end of the
program
Void main()
{
Cout<< “ enter no”.
Int a,b; // declaration of var;
Cin >>a>>b ;
Cout<< “ addition =” <<a+b;

 Dynamic initialization of variable :-


Void main ()
{
Cout<< “ enter two no” ;
Int a,b;
Cin >> a>>b;
Int c == a+b; // dynamic initialization
Cout<< addition =” <<c;

WAP to find area of circle


# include ( iostream.h)
void main()
{
Const float pi = 3.14;
Cout<<” enter radius = 11;

5
int r;
cin >> r;
Float a = pi * r* r;
Cout << ‘’ area =’’<<a;
}

 Reference variable :-
Int var1;
Int & var2 = var1;
Int a=10;
Int & b=a;
 Memory management operator –
new
pointer var = new datatype;
int * p = new int
float * p = new float;
c++ support new & delete operator perform the tasks of allocating & and
destroying the memory in a better & easier way. This operators are known as
memory management operator the objects can be created using ‘new’
operator &then destroy using delete operator. new’ operator can be used to
create object of any datatype pointer variable is a pointer to that datatype the
new operator allocates efficient memory to hold the data object of that
datatype & return the address of object.
We can also initialize the memory uses in new operator.

New operator for one dimensional array-

Pointer variable = new datatype [ size ];


Int * p = new int [5]
Here size specifies the no. of elements in array. In the e.g. memory space
allocated for n array of 5 element & so on.

 New operator for multi-dimensional array –

Pointer var = new datatype [S1] [S2] [S3];


Int *p = new int [S] [2] [3]
Pointer variable require 60 bytes for 30 elements.

Delete operator –

When a data object is no longer needed, It is destroy to reuse the memory


space for this purpose, we need delete operator.

delete pointervar;
int *p=new int;
delete p;
 to destroy array memory locations –

6
int *p = new int [5];
delete [ ] p;

 Manipulator
1) endl
cout << “Hello” <<endl;
cout <<”world”;
Hello
World
Manipulators are operator that are used to format the data display.
Endl is used in output statements. It has same effect like in.
2) Set W
Setw ( column width);
Cout << setw(10);
Cout << “Hello”;

H E L L O

By default the output is left aligned. If we have to specify common filed width
for all the numbers then we use setw manipulator.

FUNCTIONS
Function prototype –
Returntype functionname (Argument list);
(dt1 v1, dt2 v2…….);
Function prototype is one of the major improvements added in C++ function.
Prototype describe such as number of argument type of argument and return
data type with function prototype a template is always used when declaring
and defining a function when function is called, compiler uses this template to
ensure that proper arguments are passed & the return value is treated
correctly. Any difference will be caught by complier at the time of compilation.
The argument list contains the type of variable which is to be passed through
the function, Note that each datatype must be declared independently inside
parenthesis ()
 Inline function –
One of the objectives of using function is to save memory space likely
to be called many times. However every time function is called it takes a lot of
extra time in executing the series of instructions for tasks such as jumping to
the function saving registers, pushing argument into the stack & returning to
the calling function. One solution to this problem is to use macro definition.
The major drawbacks with macros are that they are not really functions and
the usual error check doesn’t occur during compilation.
Therefore C++ introduces new function called inline function. It is
expanded in line when it is called i. e. compiler replace the function call to the

7
correspondence function code. To declare inline function, inline keyboard is
used in definition part of function.
Inline return datatype function name (dt1 ver1, dt2 ver2)
{
_____________
_____________
_____________
}

Void add (int a, int b);


Main()
{ clrscr(); Inline void add
Int a-=2; (int a, int b)
Int b= 2; {
Add (a, b); Count << “Addition” << a + b
} }
Default argument in functions –
|| declaration
Void add (int a, int b=2);
Void add (int a,
Void main ()
(intb)
{
{
Int a = 3;
Int b = 5; cout << “Addition “
add (a,b); || calling 1 << a+b;
add(a); || calling 1 }
}

The default argument is always specified in the declaration of function.


In above program – for first calling start
3 5
add (a, b)
a=3, b=6, These values are passed to the definition of add function the output is
addition = 8
In the second calling function – i.e. add (a) a=3 only ‘a’ variable valve is passed to
add function, since second value is not specified variable value will be taken from
default value i.e. b=2
Therefore output will be = 5.
FUNCTION OVERLOADING
Overloading refers to the use of some thing for different purposes. C++ also permits
overloading of function. This means that we can use same function name to create
functions that perform a variety of different tasks. This is also known as function
polymorphism in C++. Using the concept of function overloading, we can design a
family of functions with same function name but with different operations depending
on the argument list correct function will be called is determined by arguments.
Function call first matches a prototype declaration having the same number & type of
arguments & then call appropriate functions for executions.

8
WAP to find area of circle, triangle & square using function overloading.
Void area (int r); || declaration
Void are (int b, int h); || declaration
Void area (float s); || declaration
Const & float pi = 3.14;
Void main()
{
Int r ;
cout << “ Enter radius”;
cin >> r;
area (r) ; || calling
int b, h;
cout << “ Enter base & height”;
cin >> b >> h;
area (b,h); || calling
float s;
cout << “Enter side”;
cin >> s;
area(s);
}
void area (int r)
{
int a = pi *r*r;
cout <<” area of circle = “ ,,a;
}
void area (int b, int h)
}
Int a = b *h** 0.5,
Cout < <” Area of Triangle = “ < < a;
}

9
Chapter 3
Constructors and Destructors
Constructors:-
It is a special member function used for initialization of an object of its class. It
is special because its name is the same as classname. The constructor is called
whenever object of that class is created. It is called as a constructor because it
construct the values of data members of class.

 Characteristics :-

(1) They should be declared in public section of class.


(2) They are called when object is created.
(3) They do not have any return data type not even void i.e. they can not return
any value.
(4) They can not be inherited though the derived class can call the base class
constructor
(5) They can use default arguments.
(6) Constructor can not be virtual.

* Default constructor:-
Syntax
Class name c)
{
__________
__________
__________
}
Create default constructor for emp class-
Class emp
{
Int empid;
Float salary;
Char name [20];
Public :
Emp () // default constructor
{
Empid ;
Salary =
Strcpy (name,” “);
}
Void getdata ()
{
Cin>>empid>>salary>>name;
}

10
Void putdata ()
}
Cout<<empid<<salary<<name;
}
};
Void main()
{
Emp e1; // calling default constructor
E1. Getdata (); // calling member function
E1. Putdata();
}

 Parameterizes Constructor

This type takes argument values. It is also known as constructor overloading


Class emp
{
Int id;
Float salary;
Char name[20]
Public:
Emp (int x, float y, char z[ ] ) // Parameterizes constructor
{
Id = x;
Salary=y
Strcpy (name,z);
}
Emp( ) // default constructor
{
Id=
Salary=
Strcpy (name, “ “ );
}
Void putdata ()
{
Cout <<id<<salary<<name;
}
};
Void main( )
{
Emp e1; // calling default constructor.
Emp e2 (2,8000.50,”kk” ) // calling parameter const.
E1. Putdata ();
E2.putdata ();
}

11
Copy constructor / constructor overloading
It takes object as an argument
Class emp
{
Int id;
Float salary;
Char name [20];
Public:
Emp () // default constructor
{
Id =
Salary=
Strcpy = (name, “ “);
}
emp (int x, float y, char2[ ] ) // parameter const
{
Id=x;
Salary=y;
Strcpy (name,2);
}
emp (emp &e1 ) // copy constructor
{
Id = e1.id;
Salary=e1.slary;
Strcpy(name, e1 name);
}
};
Void main ( )
{ emp e1; // call to D.C.
Emp e2 (20,2000.30, “55”) // calling parameter constructor
Emp e3 (e2); // calling to copy const
}

12
 Default argument in constructor :-

Definition of constructor outside class syntax


Classname: : class name ( )
{
_________
_________
_________
}
Class average
{
Int a,b;
Public:
Average (); // default constructor
Average (int x1, int y==6) // parameter constructor default argument.
}
Average: : average () // definition of D.C.
{
A=
B=
}
Average: : average (int x, int y) // definition of p.c.
{
A=x;
B =y;
}
Void main ()
{
Average a1; // call default constructor
Average a2(10,20); // call parameter constructor
Average a3 (10) ; // call to p.c.
}

 Destructor :-

Classname ()
{
__________
__________
__________

Destructor as name implies is used to destroy the objects that have been
created by constructor like a constructor destructor is a member function whose
name is the same as class name but it is preceded by tilde ( ) operator.

A destructor never takes an argument nor does return any values. It will be
called by complier implicitly upon exit from the program to clean up storage i.e. no
longer accessible.

13
e.g. whenever we use new operator in constructor delete operator should be used in
destructor.

Class emp

Int id;

Public:

emp () // default constructor

Id=6;

emp () // destructor

Void main()

emp eg;` // calling default const.

getch(); // calling destructor

Chapter 4
INHERITANCE

14
Reusability is an another important feature of oops. It is always nice if
we could reuse something that already exists rather than trying to create the
same all over again, Reuse of a class that has already been tested, debug &
use many times can save us the effort of developing & testing the same
again. The mechanism of deriving a new class from old one is called
Inheritance.
The old class is referred as base class or parent class & the new class
is called as subclass or child class or derived class.

Syntax
Class childclassname : visibility mode parentclass
{
Private :
data member ;
public:
member function ();
};
Type of inheritance

(1) Single inheritance


CLASS A
A INT X.Y
Void get data ( )
CLASS B
Int 2
B

Int X.Y
getdata ( )

Class A
{
Public:
int x, y;
void getdata();
};
Class B: Public A
{
Public :
Int 2;

15
};
void main()
{
B b1;
b1.z=2; || public D. M.
b2.x=2; || member derived from parent
b1.y=10; || class
b1.getdata ();
}

(2) Hierarchical
A

B C

Class A
{
Public:
Int x,y;
}

Class B : Public A
{
Public:
Int z;
};
Class C: public A
{
Public:
Class A
int w;
Int X.Y.
};
Void Get data ( )
Void Main()
)
{
B b1;
C c1;
Class B Class C
b1.x=2; //access from inheritance Int Z Int w
b1.y=2; // access from inheritance
b1.z=4; //public data member
b1.w=10 //no connection

16
c1.x=2; //access from inheritance
c1.y=2; //access do inheritance
c1.w=40; //public data member
c1.2=4; //no connection
b1. getdata ();
c1. getdata () ;
}

(3) multilevel inheritance

A Int x.y void get data ( )

B
Int Z

C
Int W
Class a

Int x,y
class A
{
Public: Class B
Int x; Int Z
Void getdata ();
};
Class b: public A Class C
{ Int W
Public
int z;
};
Class c : public B
{
Public :
}
Void main c)
{
C c1 ;
c1.x=20; //access due to inheritance
c1.y=10; //access due to inheritance
c1.z=10; // access due to inheritance
c1.w=10; //data member
c1. getdata(); //inheritance
}

17

You might also like