You are on page 1of 20

Assignment no.

1
Title: Study of Object Oriented Programming
Introduction: Object Oriented Programming is a programming methodology that associates data
structure with a set of operators, which act upon it.

OOP is a method of implementation in which programs are organized as c-operative collections


of objects.

Features of object oriented programing are:

1. Emphasis is on data rather than procedure.


2. Programs are divided into what are known as objects.
3. Data structures are designed such that they characterized the objects.
4. Functions that operate on data of an object are tied together in the data structure.
5. Data is hidden and cannot be accessed by external functions.
6. Objects may communicate with each other through functions.
7. New data and functions can be easily added whenever necessary.
8. Follows bottom-up approach in program design.

Basic concept of object oriented programming

It is necessary to understand some of the concepts used extensively in object oriented


programming.

These include:

 Object
 Classes
 Data abstraction and encapsulation
 Inheritance
 Polymorphism
 Dynamic binding
 Message passing

Objects:

Objects are the basic runtime entities in an OOP. They may represent a person, a place, a
bank account, a table of data or any item that program has to handle. They may also represent
user defined data such as vectors, time and lists. Programming problem is analyzed in terms of
object and the nature of communication between them. Program objects should be chosen such
that they match closely with the real-world objects. Objects take up space in the memory and
have an associate address like a reward in Pascal, or Structure in C.
Classes:

The entire set of data and code of an object can be made a user-defined data type with the
help of a class. In fact, objects are variables of type class. Once a class has been defined we can
create class any number of objects belonging to that class. Each object is associated with the data
of type class with which they are created. A class is thus a collection of objects of similar type.

e.g.: mango, apple, orange are member of the class Fruit.

Fruit mango;

Will create an object mango belonging to the class Fruit.

Data Abstraction and Encapsulation:

The wrapping up of data and functions into a single unit (called class) is known as
encapsulation. Data encapsulation is the most striking feature of a class. The data is not
accessible to the outside world, and only those functions which are wrapped in the class can
access it. These functions provide the interface between object data and program. This insulation
of data from direct access y the program is called hiding or information hiding.

Abstraction refers to the act of representing essential features without including the
background details or explanations. Classes use the concept of abstraction and are defined as a
list of abstract attributes such as size, weight and cost and functions to operate on these
attributes. They encapsulate all the essential properties of the objects that are to be created. The
attributes are sometimes called data members because they hold information. The functions that
operate on these data are sometimes called methods or member functions. Since the classes use
the concept of data abstraction, they are known as Abstract Data Types (ADT).

Inheritance:

Inheritance is the process by which objects of one class acquire the properties of the
objects of another class. It supports the concept of hierarchical classification. For example, the
bird ‘robin’ is part of class ‘flying bird’ which is again a part of the class ‘bird’. The principle
behind this sort of division is that each derived class shares common characteristics with the
class from which it is derived as illustrated in fig.

The concept of inheritance provides the idea of reusability. This means that we can add
additional features to an existing class without modifying it. This is possible by deriving new
class from an existing class the new class will have the combined features of both the classes.

Polymorphism:
Polymorphism is another important OOP concept. Polymorphism means the ability to
take more than one form. An operation may exhibit different behaviors in different instance. The
behavior depends upon the types of data used in the operation. For example consider the
operation of addition. For two numbers the operation will generate the sum. If the operands are
strings, then the then the operation would produce a third string by concatenation. The process of
making an operator to exhibit behaviors in different behaviors in different instances is known as
operator overloading.

Dynamic Binding:

Binding refers to the linking of procedure call to the code to be executed in response to
the call. Dynamic Binding (also refer as late binding) means that the code associated with a
given procedure call is not known until the time of call at run-time. It is associated with
polymorphism and inheritance. A function call associated with a polymorphic reference depends
on the dynamic type of that reference.

Message Passing:

Objects communicate with one another by sending and receiving information much the
same way as people pass messages to one another. The concept of message passing makes it
easier to talk about binding systems that directly model or simulate their real world counterparts.

A message for an object is a request for execution of a procedure, and therefore will invoke a
function in the receiving object that generates the desire results.

Applications of OOP

 Real-time system
 Simulation and modeling
 Object-oriented databases
 Hypertext, hypermedia and expertext
 AI and expert system
 Neural networks and parallel programming
 Decision support and office automation systems
 CIM/CAM/CAD Systems.
Assignment No 2.
Title: Study of Control Statements and Decision Making Statements
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. C++ is rich in built-in operators and provides the following types of operators:
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Misc Operators

Control Structure

Decision Making Statements:

1. If statement:

It is powerful decision making statement. It is used to control flow of execution of


statement. It is basically two way decision statement is as follows:
if (test expression)
{
Statement block;
}
Statement x;

The statement block may be single statement or group of statements. If test expression is
true then statement block will be executed otherwise statement block will be skipped and
program execution is transferred to statement x. when condition is true, both the
statement block and statement x are executed in sequence.

2. If-else statement:

It is an extension of simple if statement. The format of this statement is as follows:


if (test expression)
{
True block statements;
}
else
{
False block statements;
}
Statement x;
If the test expression is true then the true block statements are executed otherwise false
block statements are executed. In either case either true block statements or false bock
statements are executed and not both.

3. Nesting of if-else statement:

When series of decisions are involved, we have to use more than one if-else statement in
nested form.

Format:
if(test condition-1)
{
if(test condition-2)
{
Statement-1;
}
else
{
Statement-2;
}
}
else
{
Statement-3;
}
Statement-x;

4. else-if ladder:

The format of else-if ladder is as given below. In this structure, conditions are executed
from top. As soon as true condition is found, the statement associated with it is executed
and then program control is transferred to statement x. When all the conditions are false
the default statement is executed and then program control is transferred to statement-x.

Format:
if(condition-1)
{
Statement-1;
}
else if(condition-2)
{
Statement-2;
}
else if(condition-n)
{
Statement-n;
}
else
{
Default statement;
}
Statement-x;

Loop statements:

Whenever you want to repeat some task (set of instructions) for number of times we
prefer loop instead of writing all those instructions for number of times. In looping sequence of
statements are executed until some conditions for termination of loop are satisfied.

For example, printing Hello 10 times will require either 10 cout’s repeated 10 times or a loop. By
writing loop programmer avoids repetitive coding and also length of program reduces.

a. While loop:

It is simplest of all looping structure. The format is:


while (test condition)
{
Body of loop;
}

While loop repeats a statement or group of statements inside loop while a given condition is
true.

b. do…while loop:

In while loop the test condition is evaluated first therefore body of loop may not execute if
condition is false at very first attempt. On some occasion it is necessary to execute the body
of loop before test is performed. Hence do..while loop is used. The format is:
do
{
Body of loop
} while(condition);

c. for loop:

in for loop initialization, condition checking and loop expression are specified in single line.
The format of for loop is:
for (initial expression; loop condition; loop expression)
{
Body of loop;
}
Assignment No :3
Title: Study of classes and objects
Introduction

The most important feature of C++ is ‘Class’. The class is an extension of idea of structure. It is
a new way of creating and implementing user-defined data type.

Structures:

In C++ structures can have both variables and functions as the member. It can also declare some
of its member as ‘private’ so that they cannot be accessed directly by the external functions.

In C++ the structure names are stand-alone and can be used like any other type names. In other
words the keyword struct can be omitted in the declaration of structure variables. For example
we can declare the student variable A as

student A; //C++ declaration

Class:

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. When defining a class we are creating a
new Abstract Data Type that can be treated like any other built-in data type. Generally, a class
specification has two parts:

1. Class declaration
2. Class function definitions

The class declaration describes the type and scope of its members. The class function definitions
describe how the class functions are implemented.

The general form of class declaration is:

class class_name
{
private:
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
};

The keyword class specifies that what follows is an abstract data of type class_name. The body
of class is enclosed within braces and terminated by a semicolon. The class body contains the
declaration of variables and functions. These functions and variables are collectively called as
class members. They are usually grouped under two sections, namely, private and public to
denote which of the members are private and which of them are public. In C++, the keywords
private and public are called access specifiers. The data hiding concept in C++ is achieved by
using the keyword private. Private data and functions can only be accessed from within the class
itself. Public data and functions are accessible outside the class also.

Defining object of class:

The objects of a class are declared after the class definition. One must remember that a class
definition does not define any objects of its type, but it defines the properties of a class. For
utilizing the defined class, we need variables of the class type. For example,

Largest ob1,ob2; //object declaration

will create two objects ob1 and ob2 of largest class type. As mentioned earlier, in C++ the
variables of a class are known as objects. These are declared like a simple variable i.e., like
fundamental data types.

Accessing a member of class:

We can access the data members and member function by using dot (.) operator. For example,

O2.function1();

This will call function1 function inside the class for object O2.

Similarly the data members can be accessed as:

O2.data1 =5.5;

Member Function Definition

Inside Class Definition

When a member function is defined inside a class, we do not require to place a membership
label along with the function name. We use only small functions inside the class definition and
such functions are known as inline functions.

In case of inline function the compiler inserts the code of the body of the function at the place
where it is invoked (called) and in doing so the program execution is faster but memory penalty
is there.

Outside Class Definition Using Scope Resolution Operator (::)

In this case the function’s full name (qualified_name) is written as shown:

Name_of_the_class :: function_name

The syntax for a member function definition outside the class definition is :
return_type name_of_the_class::function_name (argument list)

body of function

Here the operator::known as scope resolution operator helps in defining the member function
outside the class. Earlier the scope resolution operator(::)was used on situations where a global
variable exists with the same name as a local variable and it identifies the global variable.
Assignment No: 4
Title: Study of Pointers, Array and Friend function
Use of pointers with array:

Pointers are the variables that hold address. Not only can pointers store address of a single
variable, it can also store address of cells ofan array.When we declare an array, compiler
allocates continuous blocks of memory so that all the elements of an array can be stored in that
memory. The address of first allocated byte or the address of first element is assigned to an array
name. Thus array name works as pointer variable.The address of first element is also known as
base address.Array is a collection of values of similar type. It can also be a collection of
references of similar type.

Function Pointer in C++

Like normal variable, pointer variable can be passed as function argument and function can
return pointeras well.

There are two approaches to passing argument to a function:

Call by Value

Call by Reference / Address

Call by Value

In this approach, the values are passed as function argument to the definition of function.

Call by Reference

In this approach, the references/addresses are passed as function argument to the definition of
function.

Difference b/w Call by Value and Call by Reference:

Call by Value Call by Reference

The actual arguments can be variable or The actual arguments can only be variable.
constant.

The values of actual argument are sent to The reference of actual argument is sent to
formal argument which is normalvariables. formal argument which is pointervariables.

Any changes made by formal arguments will Any changes made by formal arguments will
not reflect to actual arguments. reflect to actual arguments.
Friend Function:

Suppose there are two classes Manager and Accountant having their own data members and
member functions. We want a non-member function income() to operate on the data members of
both these classes.

We cannot use a single common function here since such a function would not be able to access
private data members of both the classes.

For such a situation, common function to be made compatible with both the classes. Such a
function is called Friend Function.

A non-member function may be made a friend function, so that it can access the private data
members of all the classes that have declared it friend.

A function which has been declared as a friend function can access the private data of the classes
that have declared it as friend. Such a function need not be member of any class.

Declaration of friend function is:


class ABC
{
…..
public:
…..
friend void xyz(void); // declaration
};

The function declaration should be preceded the word friend.

A friend function possesses certain special characteristics:

 It is not in the scope of the class to which it has been declared as friend.
 It cannot be called using object of that class.
 It can be invoked like a normal function without the help of any object.
 It cannot access the member names directly and has to use an object name and dot
operator with each member name.
 It can be declared either in the public or the private part of a class without affecting its
meaning.
 It has the object as argument.
Assignment No: 5
Title: Study of Inheritance.
Inheritance:

Inheritance allows a class to include the members of other classes without repetition of
members. There were three ways to inheritance means, “public parts of super class remain
public and protected parts of super class remain protected.” Private Inheritance means
“Public and Protected Parts of Super Class remain Private in Sub-Class”. Protected
Inheritance means “Public and Protected Parts of Superclass remain protected in Subclass.

Inheritance is a concept which is the result of commonality between classes. Due to this
mechanism, we need not repeat the declaration as well as member functions in a class if they
are already present in another class. For example, consider the classes namely “minister” and
“prime minister”. Whatever information is present in minister, the same will be present in
prime minister also. Apart from that there will be some extra information in class prime
minister due to the extra privileges enjoyed by him. Now, due to the mechanism of
inheritance, it is enough only to indicate that information which is a specific to prime
minister in its class. In addition, the class prime minister will inherit the information of class
minister.

Types of Inheritance

SINGLE INHERITANCE

The way of deriving a class from single class. So, there will be only one base class for the
derived class.

Private Inheritance

class A{

};

class C: private A {

All the public parts of class A and all the protected parts of class A, become private
members/parts of the derived class C in class C. No private member of class A can be
accessed by class C. To do so, you need to write public or private functions in the Base class.
A public function can be accessed by any object, however, private function can be used only
within the class hierarchy that is class A and class C and friends of these classes in the above
cases.

Public Inheritance
Consider the following classes:

class A{/*........*/};

class E: public A

{ /*

};
Now, all the public parts of class A become public in class E and protected part of A become
protected in E.

Protected Inheritance

Consider the following classes:

class E: protected A

{ /*

*/

};

Now, all the public and protected parts of class A become protected in class E.

No private member of class A can be accessed by class E.

MULTIPLE INHERITANCES

A class can have more than one direct base classes.

Consider the following classes:

Class A

{
};

Class B

};

Class C : public A, public B {

};

This is called Multiple Inheritance. If a class is having only one base class, then it is known as
single inheritance. In the case of Class C, other than the operations specified in it, the union of
operations of classes A and B can also be applied.

MULTILEVEL INHERITANCE

A class is derived from another derived class. For example, class A serves as a base class for the
derived class B, which in turn serves as a base class for the derived class C. the class B known as
the intermediate base class since it provides a link for the inheritance between A and C. The
chain ABC is known as inheritance path.

A derived class with multilevel inheritance is known as follows:

class A

…….;

};

class B : public A

…..;

};

class C : public B

.…;

};
HIERARCHICAL INHERITANCE

In inheritance certain features of one level are shared by many others below that level is known
as hierarchical inheritance.

A subclass can be constructed by inheriting the properties of the base class. A subclass can serve
as a base class for the lower level classes and so on.

HYBRID INHERITANCE

There could be situations where we need to apply two or more types of inheritance to design a
program. For example, consider the case of processing the student results. Assume that we have
to give weightage for sports before finalizing the results. The weightage for sports is stored in a
separate class called sports

Virtual Base Classes

Consider a situation where all the three kind of inheritance, namely, multilevel, multiple and
hierarchical inheritance are involved. We can illustrate this with the help of fig. The ‘child’ has
two direct base classes ‘parent1’ and ‘parent2’ which themselves have a common base class
‘grandparent’. The ‘child’ inherits the traits of ‘grandparent’ via two separate paths. It can also
inherit directly as shown by the broken line. The ‘grandparent’ is sometimes referred to as
indirect base class.
Assignment No 6
Title: study on overloading: function overloading and operator overloading
Function overloading:

If any class has multiple functions with same names but different parameters then they are said to
be overloaded. Function overloading is a feature in C++ where two or more functions can have
the same name but different parameters.

Function overloading is usually used to enhance the readability of the program. If you have to
perform one single operation but with different number or types of arguments, then you can
simply overload the function.

Operator overloading

Operator overloading is an important concept in C++. It is a type of polymorphism in which an


operator is overloaded to give user defined meaning to it. Overloaded operator is used to perform
operation on user-defined data type. For example '+' operator can be overloaded to perform
addition on various data types, like for Integer, String(concatenation) etc.
Assignment no 7
Title: Study on Polymorphism
Introduction

The word polymorphism means having many forms. In simple words, we can define
polymorphism asthe ability of a message to be displayed in more than one form.

Real life example of polymorphism, a person at a same time can have different characteristic.
Like aman at a same time is a father, a husband, a employee. So same person possess have
differentbehavior in different situations. This is called polymorphism.

Polymorphism is considered as one of the important features of Object Oriented Programming.

The word polymorphism means having many forms. Typically, polymorphism occurs when there
is a hierarchy of classes and they arerelated by inheritance.

In programming languages, polymorphism means that some code or operations or objects


behavedifferently in different contexts.

Virtual Function

In object-oriented programming, in languages such as C++, a virtual function or virtual method


is an inheritableand overridable function or method for which dynamic dispatch is facilitated.
This concept is an important part ofthe (runtime) polymorphism portion of object-oriented
programming (OOP).

Virtual functions allow a program to call methods that don't necessarily even exist at the moment
the code iscompiled.

In C++, virtual methods are declared by prepending the virtual keyword to the function's
declaration in the baseclass. This modifier is inherited by all implementations of that method in
derived classes, meaning that they cancontinue to over-ride each other and be late-bound. And
even if methods owned by the base class call the virtualmethod, they will instead be calling the
derived method.

Abstract base classes

Abstract class is used in situation, when we have partial set of implementation of methods in a
class. For example, consider a class has four methods. Out of fourmethods, we have an
implementation of two methods and we need derived class to implement other two methods. In
these kind of situations, we should useabstract class.
A virtual function will become pure virtual function when you append "=0" at the end of
declaration of virtual function.

A class with at least one pure virtual function or abstract function is called abstract class.

Pure virtual function is also known as abstract function.

We can't create an object of abstract class b'coz it has partial implementation of methods.

Abstract function doesn't have body

We must implement all abstract functions in derived class.

Constructor is a special function used to initialize class data members or we can say constructor
is used to initialize the object of class.

Characteristics of constructor.

Constructor name class name must be same.

Constructor doesn't return value.

Constructor is invoked automatically, when the object of class is created.

Types of Constructor

Default Constructor

Parameterize Constructor

Copy Constructor

C++ Destructor

Constructor allocates the memory for an object.

Destructor deallocate the memory occupied by an object. Like constructor, destructor name and
class name must be same, preceded by a tilde(~) sign. Destructor take no argument and have no
return value.

Constructor is invoked automatically when the object created.

Destructor is invoked when the object goes out of scope. In other words, Destructor is invoked,
when compiler comes out form the function where an object is created.

The constructors are used to initialize member variables of the object, and the destructor is used
to destroy the object. The compiler automatically invokes constructors and destructors. The
derived class does not require a constructor, if the base class contains a zero-argument
constructor. In case the base class has a parameterized constructor, then it is essential for the
derived class to havea constructor. The derived class constructor passes arguments to the base
class constructor. In inheritance, normally derived classes are used to declare objects. Hence, it is
necessary to define constructors in the derived class. When an object of a derived class is
declared, the constructors of the base and derived classes are executed.

You might also like