You are on page 1of 14

EC312 Object Oriented Programming Module 1

MODULE I
Concepts of OOP – Introduction to OOP, Procedural Vs. Object Oriented Programming, Principles of OOP,
Benefits and applications of OOP.
Beginning with C++: Overview and Structure of C++ Program, Classes and Objects, Constructors and
Destructors.

BENEFITS OF OOP
• Through inheritance, we can eliminate redundant code extend the use of existing classes.
• To build programs from the standard working modules that communicate with one another,
rather than having to start writing the code from scratch. This leads to saving of development
time and higher productivity.
• The principle of data hiding helps the programmer to build secure program that cannot be
invaded by code in other parts of a programs.
• It is possible to have multiple instances of an object to co-exist without any interference.
• It is possible to map object in the problem domain to those in the program.
• It is easy to partition the work in a project based on objects.
• The data-centered design approach enables us to capture more detail of a model can
implemental form.
• Object-oriented system can be easily upgraded from small to large system.
• Message passing techniques for communication between objects makes to interface
descriptions with external systems much simpler.
• Software complexity can be easily managed.

APPLICATIONS OF OOP
• Real-time system
• Simulation and modeling
• Object-oriented data bases
• Hypertext, Hypermedia, and expertext
• AI and expert systems
• Neural networks and parallel programming
• Decision support and office automation systems
• CIM/CAM/CAD systems

PROCEDURAL VS OBJECT ORIENTED PROGRAMMING


Procedure Oriented Programming Object Oriented Programming

Divided Into In POP, program is divided into small In OOP, program is divided into parts
parts called functions. called objects.

Importance In POP, Importance is not given to In OOP, Importance is given to the data
data but to functions as well as rather than procedures or functions

Ambily Mohan, Dept of CSE, JBCMET Page 1


EC312 Object Oriented Programming Module 1

sequence of actions to be done. because it works as a real world.

Approach POP follows Top Down approach. OOP follows Bottom Up approach.

Access POP does not have any access OOP has access specifiers named Public,
Specifiers specifier. Private, Protected, etc.

Data Moving In POP, Data can move freely from In OOP, objects can move and
function to function in the system. communicate with each other through
member functions.

Expansion To add new data and function in POP OOP provides an easy way to add new
is not so easy. data and function.

Data Access In POP, Most function uses Global In OOP, data cannot move easily from
data for sharing that can be accessed function to function, it can be kept public
freely from function to function in the or private so we can control the access of
system. data.

Data Hiding POP does not have any proper way for OOP provides Data Hiding so provides
hiding data so it is less secure. more security.

Overloading In POP, Overloading is not possible. In OOP, overloading is possible in the


form of Function Overloading and
Operator Overloading.

Examples Example of POP are : C, VB, Example of OOP are : C++, JAVA,
FORTRAN, Pascal. VB.NET, C#.NET.

INTRODUCTION TO C++
 C++ is an object-oriented programming language.
 It was developed by Bjarne Stroustrup at AT&T Bell Laboratories.
 C+ + is a superset of C.
 The most important facilities that C++ adds on to C care classes, inheritance, function
overloading and operator overloading. These features enable creating of abstract data types,
inherit properties from existing data types and support polymorphism, thereby making C++ a
truly object-oriented language.

Ambily Mohan, Dept of CSE, JBCMET Page 2


EC312 Object Oriented Programming Module 1

Application of C++
 C++ is a versatile language for handling very large programs; it is suitable for virtually any
programming task including development of editors, compilers, databases, communication
systems and any complex real life applications systems.
 Since C++ allow us to create hierarchy related objects, we can build special object-oriented
libraries which can be used later by many programmers.
 While C++ is able to map the real-world problem properly, the C part of C++ gives the
language the ability to get closed to the machine-level details.
 C++ programs are easily maintainable and expandable. When a new feature needs to be
implemented, it is very easy to add to the existing structure of an object.
 It is expected that C++ will replace C as a general-purpose language in the near future.

1. Simple C++ Program that prints a string on the screen.

/* Program to print a string on the output screen */


#include<iostream.h> //Header file
#include<conio.h> //Header file

using namespace std;


void main()
{
cout<<”Hello World \n”; //This is an output statement
getch();
}

Output
Hello World

 Like C, the C++ program is a collection of function. The above example contain only one
function main(). As usual execution begins at main(). Every C++ program must have a main().
C++ is a free form language. With a few exception, the compiler ignore carriage return and
white spaces.
 Like C, the C++ statements terminate with semicolons.
Comments
 C++ introduces a new comment symbol // (double slash). Comment start with a double slash
symbol and terminate at the end of the line. A comment may start anywhere in the line, and
whatever follows till the end of the line is ignored. Note that there is no closing symbol. The
double slash comment is basically a single line comment.
 Multiline comments can be written as /* */
Output operator
 The only statement in the above program is an output statement.
 This statement introduces two new C++ features, cout and <<.

Ambily Mohan, Dept of CSE, JBCMET Page 3


EC312 Object Oriented Programming Module 1

 The identifier cout(pronounced as C out) is a predefined object that represents the standard
output stream in C++. Here, the standard output stream represents the screen. It is also possible
to redirect the output to other output devices. The operator << is called the insertion or put to
operator. The string in quotation marks to be displayed on the screen.

The iostream File


 The #include directive instructs the compiler to include the contents of the file enclosed within
angular brackets into the source file. The header file iostream.h should be included at the
beginning of all programs that use input/output statements.

Namespace
 Namespace is a new concept introduced by the ANSI C++ standards committee.
 This defines a scope for the identifiers that are used in a program. For using the identifier
defined in the namespace scope we must include the using directive
 using namespacestd;
 Here, std is the namespace where ANSI C++ standard class libraries are defined. All ANSI
C++ programs must include this directive. This will bring all the identifiers defined in std to the
current global scope.
 Using and namespace are the new keyword of C++.

Return Type of main()


 In C++, main () returns an integer value to the operating system. Therefore, every main () in
C++ should end with a return (0) statement; otherwise a warning an error might occur. Since
main () returns an integer type for main () is explicitly specified as int.
 The default return type for all function in C++ is int.

2. Write a C++ program to read two numbers from the keyboard and display their average on
the screen.

#include<iostream.h> // include header file


#include<conio.h> // include header file
using namespace std;

void main()
{
float n1, n2,sum, avg;

cin>> n1; // Read Numbers from keyboard


cin>> n2;

sum = n1 + n2;
avg = sum/2;

cout<< ”Sum = “ << sum << “\n”;


cout<< “Average = “ <<avg<< “\n”;
Ambily Mohan, Dept of CSE, JBCMET Page 4
EC312 Object Oriented Programming Module 1

getch();
}
Output
Enter two numbers: 6.5 7.5
Sum = 14
Average = 7
FUNCTION
 Functions are the building blocks of C++ programs.
 Function is a collection of declarations and statements.
Need for a Function
 Monolethic program (a large single list of instructions) becomes difficult to understand. For this
reason functions are used.
 A function has a clearly defined objective (purpose) and a clearly defined interface with other
functions in the program.
 Reduction in program size is another reason for using functions. The functions code is stored in
only one place in memory, even though it may be executed as many times as a user needs.
Function prototype
 Function prototype is the skeleton of a function.
 Syntax is
return_type function_name(type of arguments);
 Example:
int add(int, int);
void display(void);
Function Definition And Declaration
 In C++, a function must be defined prior to it’s use in the program.
 The function definition contains the code for the function.
 The general syntax of a function definition in C++
Return_type function_name(list of arguments with their types)
{
//Function Body
}
 Return_type specifies the type of the value to be returned by the function.
 Function_name is a valid C++ identifier (no reserved word allowed) defined by the user and it
can be used by other functions for calling this function.
 Argument list is a comma separated list of variables of a function through which the function
may receive data or send data when called from other function. When no parameters, the
argument list is empty.
Function Call
 A function gets called when the function name is followed by a semicolon.
 Syntax is
Ambily Mohan, Dept of CSE, JBCMET Page 5
EC312 Object Oriented Programming Module 1

function_name(list of arguments);
 Example:
sum(x,y);
display(mark);

1. Write a c++ program to add two numbers using function

#include<iostream.h>
#include<conio.h>

void add(int, int); //Function prototype

void main()
{
int x,y;
cout<<”Enter two numbers”;
cin>>x;
cin>>y;
add(x,y); //Function call
}

void add(int a, int b) //function definition add()


{
int sum;
sum=a+b;
cout<<”\nThe sum of two numbers is “<<sum;
getch();
}
STRUCTURE OF C++
 A typical C++ program would contain four sections.

 It is a common practice to organize a program into three separate files.


 The class declarations are placed in a header file

Ambily Mohan, Dept of CSE, JBCMET Page 6


EC312 Object Oriented Programming Module 1

 The definitions of member functions go into another file. This approach enables the programmer
to separate the abstract specification of the interface from the implementation details (member
function definition).
 Finally, the main program that uses the class is places in a third file which “includes: the previous
two files as well as any other file required.
 This approach is based on the concept of client-server model

 The class definition including the member functions constitute the server that provides services to
the main program known as client. The client uses the server through the public interface of the
class
CLASSES AND OBJECTS
 A class is a way to bind the data and its associated functions together. It allows the data and
functions to be hidden, if necessary, from external use.
 Class declaration

 The variables declared inside the class definition are known as data members and the functions
declared inside a class are known as member functions.

Ambily Mohan, Dept of CSE, JBCMET Page 7


EC312 Object Oriented Programming Module 1

 Wrapping of data and function and function into a single unit (i.e. class) is known as data
encapsulation.
 By default the data members and member function of a class are private.
 Private data members can be accessed by the functions that are wrapped inside the class
 A member function can be defined:
(i) Inside the class definition
(ii) Outside side the class definition using scope resolution operator (::).
 To access member of a class dot operator is used.
Object_name.data_member;
Object_name.member_function;
 Outside Class Definition Using Scope Resolution Operator (::)
 Syntax:
return_type name_of_the_class :: function_name (argument list)
{
//body of function
}
 Application of Scope resolution operator (::)
 It is used to specify scope of a member function.
 It is used to access a global variable.

2. Write a program to find sum of two integers using class and object.

#include<iostream.h>
#include<conio.h>

class add
{
int x, y, z;
public:
void getdata()
{
cout<<”Enter two numbers”;
cin>>x>>y;
}
void calculate(void);
void display(void);
};

void add :: calculate()


{
z=x+y;
}

void add :: display()


{
Ambily Mohan, Dept of CSE, JBCMET Page 8
EC312 Object Oriented Programming Module 1

cout<<”Result = “<< z;
}

void main()
{
add a;
a.getdata();
a.calculate();
a.display();
}

Output:
Enter two numbers 5 6
Result = 11
CONSTRUCTOR
 A constructor is a special member function whose task is to initialize the object of a class.
 Its name is same as the class name.
 A constructor does not have a return type.
 A constructor is called or invoked when the object of its associated class is created.
 It is called constructor because it constructs the values of data members of the class.
 A constructor cannot be virtual.
 A constructor can be overloaded.
 There three types of constructor:
1. Default Constructor
2. Parameterized Constructor
3. Copy constructor
Default Constructor
 The constructor which has no arguments is known as default constructor
 It is used to initialize the data members to some default values.
#include<iostream.h>
#include<conio.h>

class Add
{
int x, y, z;
public:
Add(); //Default Constructor
void calculate();
void display();
};

Add::Add()
{
x=6;
y=5;
Ambily Mohan, Dept of CSE, JBCMET Page 9
EC312 Object Oriented Programming Module 1

void Add :: calculate()


{
z=x+y;
}

void Add :: display()


{
cout<<”Result = “<< z;
}

void main()
{
Add a;
a.calculate();
a.display();
getch();
}

Output
Result = 11

Parameterized constructor
 The constructor which takes some argument is known as parameterized constructor.
 A parameterized constructor can be called:
(i) Implicitly:
 Syntax:
Class_name object_name(list of arguments);
 Eg : Add a(5, 6);
(ii) Explicitly:
 Syntax:
Class_name object_name= Class_name(list of arguments);
 Eg: Add a=Add(5, 6);
 If the constructor has one argument, then we can also use object-name = value-of-argument;
instead of object-name (value-of-argument); to initialize an object.
#include<iostream.h>
#include<conio.h>

class Add
{
int x, y, z;

public:
Add(int, int); //Parameterized constructor
Ambily Mohan, Dept of CSE, JBCMET Page 10
EC312 Object Oriented Programming Module 1

void calculate(void);
void display(void);
};

Add :: Add(int a, int b)


{
x=a;
y=b;
}

void Add :: calculate()


{
z=x+y;
}

void Add :: display()


{
cout<<”Result = “<<z;
}

void main()
{
Add a(5, 6);
a.calculate();
a.display();
getch();
}

Output
Result = 11
Copy Constructor
 The constructor which takes reference to its own class as argument is known as copy constructor.

#include<iostream.h>
#include<conio.h>

class Add
{
int x, y, z;

public:
Add()
{
}

Add(int a, int b)
{
x=a;
y=b;

Ambily Mohan, Dept of CSE, JBCMET Page 11


EC312 Object Oriented Programming Module 1

Add(Add &); //copy constructor


void calculate(void);
void display(void);
};

Add :: Add(Add &p)


{
x=p.x;
y=p.y;
cout<<”Value of x and y for new object: ”<<x<<” and ”<<y;
}

void Add :: calculate()


{
z=x+y;
}

void Add :: display()


{
cout<<z;
}

void main()
{
Add a(5, 6);
Add b(a);
b.calculate();
b.display();
getch();
}

Output:
Value of x and y for new object are 5 and 6
Result = 11
DESTRUCTOR
 It is a special member function which is executed automatically when an object is destroyed.
 Its name is same as class name but it should be preceded by the symbol ~.
 It cannot be overloaded as it takes no argument.
 It is used to delete the memory space occupied by an object.
 It has no return type.
 It should be declared in the public section of the class.

#include<iostream.h>
#include<conio.h>

class myclass
{
Ambily Mohan, Dept of CSE, JBCMET Page 12
EC312 Object Oriented Programming Module 1

public:
int x;
myclass(int i);
~myclass();
};

myclass::myclass(int i)
{
x=i;
}

myclass::~myclass()
{
cout<<"Destructing Object";
cout<<"Value of x = "<<x;
}

void main()
{
myclass t1(5);
myclass t2(19);
cout<<t1.x<<" "<<t2.x;
getch();
}
Output

Constructor Destructor

Purpose Constructor is used to initialize the Destructor destroys the objects when they
instance of a class. are no longer needed.

When called Constructor is Called when new Destructor is called when instance of a
instance of a class is created. class is deleted or released.

Memory Constructor allocates the memory. Destructor releases the memory.


Management

Ambily Mohan, Dept of CSE, JBCMET Page 13


EC312 Object Oriented Programming Module 1

Arguments Constructor accepts argument Destructor does not accepts any argument.

Order of Constructor are called in Destructor are called in reverse order of


execution successive order. constructor.

In numbers There can be multiple constructor But there is always a single destructor in
in the class. the class.

Over loading Constructors can be overloaded . Destructor cannot be overloaded.

Name Constructor has the same name as Destructor also has the same name as class
class name. name but with (~) tiled operator.

Syntax Class_Name(Arguments) ~Class_Name()


{ {
//Body of Constructor //Body of Destructor
} }

Ambily Mohan, Dept of CSE, JBCMET Page 14

You might also like