Professional Documents
Culture Documents
PREPARED BY,
M.P.Geetha,
Department of CSE
CONTENTS
Basic Concepts and benefits of OOP – Tokens –
Keywords - Identifiers – Basic data types – Derived data types
– Reference variables – Type modifiers – Type casting –
Operators and control statements – Input and output
statements. Classes and Objects – Class specification – Member
function definition – Constructors – Parameterized
constructors – Overloaded Constructors – Constructors with
default arguments – Copy constructors – access qualifiers –
Static data members and member functions – Instance
creation – Array of objects – Introduction to friend function –
Destructors.
High-Level Languages
Procedural languages
Object-Oriented languages (OOP)
3
Procedural Languages
Early high-level languages are typically called
procedural languages.
Procedural languages are characterized by sequential
sets of linear commands. The focus of such languages
is on structure.
Examples include C, COBOL, Fortran, LISP, Perl,
HTML, VBScript
4
Object-Oriented Languages
Most object-oriented languages are high-level
languages.
The focus of OOP languages is not on structure, but on
modeling data.
Programmers code using “blueprints” of data models
called classes.
Examples of OOP languages include C++, Visual
Basic.NET and Java.
5
PROCEDURAL PROGRAMMING
Procedural programming is a classic programming
where the program language is used to tell the computer
EXACTLY what to do - step by step.
A program in a procedural language is a list of
instruction. That is, each statement in the language tell the
computer to do something.
The focus of procedural programming is to break
down a programming task into a collection of
variables, data structures, and subroutines.
In PPL, functions are unrestricted access to global data.
Two type of data
Local data (that is hidden inside a function)
Global data (that can be accessed by any function in the
program).
Problem-larger number of potential connections between
functions and data as shown in following Fig.
9
CONCEPTS OF OOP
CLASSES: A class is an object-oriented concept which is used to
describe properties and behavior of a real world entity.
A class is a combination of state (data) and behavior (methods).
In object-oriented languages, a class is a data type, and objects are
instances of that data type.
In other words, classes are prototypes from which objects are
created.
It has the data and its associated function wrapped in it.
The variables declared inside a class are known as "Data Members"
and the functions are known as "Member Functions".
For example, a class Human, which is a collection of all humans in the world.
Attributes Name
Hair color
talking
Operations
walking
Circle
Radius
center
Calculate area()
Draw()
SYNTAX
class class-name
{
private:
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
};
Example:
#include <iostream.h>
class Square
{
private: void main()
int side, a; {
public:
Square x;
int area( side)
x.area(10);
{
a =side*side;
x.show();
return a; }
}
void show() Result:
{
cout << "The area is::" << a;
The area is:: 100
}
};
OBJECTS:
An object is an instance of a class. An object can
communicate with other objects using messages. An object passes a
message to another object, which results in the invocation of a
method. Objects then perform the actions that are required to get a
response from the system.
Data Members: A class contains data members. Data members are used
to store characteristics information of a real world entity in a class.
For example, name is the data member of the Human class.
Fields/attributes and methods/operation are referred to as
class members.
Fields and Methods: Objects in a class have a number of shared
properties/features/attributes.
Fields are the variables contained in a class.
Methods are functions that represent the operations associated
with a particular class.
INHERITANCE:
One of the powerful features of c++ is inheritance.
char ch;
double b;
access-specifiers…
void function-name() Class members
{
statement 1; Function or Method
statement 2;
}
}
main()
{
class-name.obj-name;
}
Object of class
EXAMPLE
#include <iostream.h>
class Cube
{ void main()
public: {
Cube x;
int cub( val)
x.cub(2);
{ x.show();
r = val*val*val; return r; }
}
void show()
Result:
{ cout << "The cube is::" << r; } The cbe is :: 8
private:
int val, r; }x;
Derived Class Visibility
Example
public:
void setcode()
{ code=++count;
}
void showcode()
Count Objects: 2
{ cout<<"\n\tObject number is :"<<code;
Object Number is: 1
}
Count Objects: 2
static void showcount()
Object Number is: 2
{ cout<<"\n\tCount Objects :"<<count;
}
};
Int stat::count;
void main()
{ stat obj1,obj2;
obj1.showcount();
obj1.showcode();
obj2.showcount();
obj2.showcode();}
Static Member function
Can access only static data members.
Static function can be called using class name
Classname::function_name(args);
Static Member function
class IDGenerator
{
private:
static int s_nNextID;
The next ID is: 1
public:
The next ID is: 2
static int GetNextID() { return
The next ID is: 3
s_nNextID++; }
The next ID is: 4
};
The next ID is: 5
int IDGenerator::s_nNextID = 1;
int main()
{
for (int i=0; i < 5; i++)
cout << "The next ID is: "
<<IDGenerator::GetNextID()
}
Friend function & friend Class
– Properties
– Friendship is granted, not taken
– NOT symmetric (if B a friend of A, A not necessarily a friend
of B)
– NOT transitive (if A a friend of B, B a friend of C, A not
necessarily a friend of C)
Friend function & friend Class
– friend declarations
friend function
Keyword friend before function prototype in class that is giving
friendship.
– friend int myFunction( int x );
friend class
Type friend class Classname in class granting friendship
o1.read_data();
class max {
int a; THIS POINTER
public:
void getdata()
{ cout<<"Enter the Value :"; cin>>a;
}
max greater(max &x)
{
if(x.a>=a)
return x;
else
return *this; Answer:
} Enter the value:45
void display() Enter the value:12
{ Maximum No is: 45
cout<<"Maximum No. is : "<<a;
} };
main()
{
max one,two,three;
one.getdata();
two.getdata();
three=one.greater(two);
three.display();
}
Constructor and Destructor
• Constructor is a special member function that has the same
name as the class name used to create, initialize object of
that class.
• Constructor is called when an object is created
• no return type.
• It should be declared in public.
class A {
int i;
public:
A(); //Constructor declared
};
A::A() // Constructor definition { i=1; }
Types of Constructor
Five types
Default Constructor
Parameterized Constructor
Overloaded Constructor
Copy Constructor
Dynamic Constructor
Default & Parameterized Constructor
• Default constructor has no argument also called no
argument constructor.
• A parameterized Constructor is one that has
parameters specified in it .
• Arguments can be passed to constructor function
when object are created.
• A constructor that can take arguments are called
parameterized constructors
EXAMPLE
class cube {
int side;
public:
cube() // default constructor
{
side=10;
}
cube(int x) //parameterized constructor
{
side=x;
} };
int main()
{
cube c;
cube c1(20);
cout << c.side;
cout << c1.side;
}
Copy Constructor
REFERENCES:
https://en.wikipedia.org/?title=Object-
oriented_programming
https://en.wikipedia.org/wiki/Data_structure
http://www.virtualians.net/page/handsout#.VYe5dVLA2-s
http://www.nptel.ac.in/courses/106104072/