Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Computers Assignment Object Oriented Programming With C++: Submitted By-Shreyankur Tripathi DUAL (AE+Av) /09/26

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

COMPUTER ASSIGNMENT

COMPUTERS ASSIGNMENT
OBJECT ORIENTED PROGRAMMING WITH C++

SUBMITTED BY-

SHREYANKUR TRIPATHI

DUAL(AE+Av)/09/26

1
SHREYANKUR TRIPATHI
COMPUTER ASSIGNMENT

DIFFERENCE BETWEEN OBJECT ORIENTED PROGRAMMING and PROCEDURE


ORIENTED PROGRAMMING.
1. In Procedure Oriented Programming(POP) language, inportance is given to the sequence of
things to be done ,i.e. algorithms and in OOP language, importance is gven to the data.
2. In POP, larger programs are divided into functions and in OOP, larger programs are divided into
objects.
3. In POP, most functions share global data, i.e. data moves freely around the system from
function to function. While in OOP, data are mostly private and only functions inside the objects
can access them.
4. POP follows the TOP-TO-DOWN approach in problem solving while OOP follows BOTTOM-TO-
TOP approach.
5. In POP, there is no access specifier while in OOP, there are public, private and protected
specifiers.
6. In POP, operators cannot be overloaded, while in OOP operator can be overloaded.

DIFFERENCE BETWEEN C AND C++ PROGRAMMING LANGUAGE.

 C does not have any classes or objects. It is procedure and function driven. There is no concept
of access through objects and structures are the only place where there is a access through a
compacted variable. c++ is object oriented.

 C structures have a different behaviour compared to c++ structures. Structures in c do not


accept functions as their parts.

 C input/output is based on library and the processes are carried out by including functions. C++
i/o is made through console commands cin and cout.

 C functions do not support overloading. Operator overloading is a process in which the same
function has two or more different behaviours based on the data input by the user.

 C does not support new or delete commands. The memory operations to free or allocate
memory in c are carried out by malloc() and free().

 Undeclared functions in c++ are not allowed. The function has to have a prototype defined
before the main() before use in c++ although in c the functions can be declared at the point of
use.

 After declaring structures and enumerators in c we cannot declare the variable for the structure
right after the end of the structure as in c++.

 For an int main() in c++ we may not write a return statement but the return is mandatory in c if
we are using int main().

2
SHREYANKUR TRIPATHI
COMPUTER ASSIGNMENT

 In C++ identifiers are not allowed to contain two or more consecutive underscores in any
position. C identifiers cannot start with two or more consecutive underscores, but may contain
them in other positions.

 C has a top down approach whereas c++ has a bottom up approach.

 In c a character constant is automatically elevated to an integer whereas in c++ this is not the
case.

 In c declaring the global variable several times is allowed but this is not allowed in c++.

3
SHREYANKUR TRIPATHI
COMPUTER ASSIGNMENT

EXPLAIN THE CONCEPT OF INHERITANCE AND POLYMORPHISM WITH EXAMPLES.

INHERITANCE
Inheritance is the process by which new classes called derived classes are created from existing classes
called base classes. The derived classes have all the features of the base class and the programmer can
choose to add new features specific to the newly created derived class.
For example, a programmer can create a base class named fruit and define derived classes as mango,
orange, banana, etc. Each of these derived classes, (mango, orange, banana, etc.) has all the features of
the base class (fruit) with additional attributes or features specific to these newly created derived
classes. Mango would have its own defined features, orange would have its own defined features,
banana would have its own defined features, etc.

This concept of Inheritance leads to the concept of polymorphism.


General Format for implementing the concept of Inheritance:

class derived_classname: access specifier baseclassname

For example, if the base class is shrey and the derived class is sample it is specified as:

Class sample: public shrey

The above makes sample have access to both public and protected variables of base class exforsys.
Reminder about public, private and protected access specifiers:

 If a member or variables defined in a class is private, then they are accessible by members of the
same class only and cannot be accessed from outside the class.
.
 Public members and variables are accessible from outside the class.
.
 Protected access specifier is a stage between private and public. If a member functions or
variables defined in a class are protected, then they cannot be accessed from outside the class
but can be accessed from the derived class.

Inheritance Example:
class shrey
{
public:
shrey(void) { x=0; }
void f(int n1)
{
x= n1*5;
}

4
SHREYANKUR TRIPATHI
COMPUTER ASSIGNMENT

void output(void) { cout<<x; }

private:
int x;
};

class sample: public shrey


{
public:
sample(void) { s1=0; }

void f1(int n1)


{
s1=n1*10;
}

void output(void)
{
shrey::output();
cout << s1;
}

private:
int s1;
};

int main(void)
{
sample s;
s.f(10);
s.output();
s.f1(20);
s.output();
}

The output of the above program is

50
200

In the above example, the derived class is sample and the base class is shrey. The derived class defined
above has access to all public and private variables. Derived classes cannot have access to base class
constructors and destructors. The derived class would be able to add new member functions, or

5
SHREYANKUR TRIPATHI
COMPUTER ASSIGNMENT

variables, or new constructors or new destructors. In the above example, the derived class sample has
new member function f1( ) added in it. The line:

Sample s;

creates a derived class object named as s. When this is created, space is allocated for the data members
inherited from the base class shrey and space is additionally allocated for the data members defined in
the derived class sample.

The base class constructor shrey is used to initialize the base class data members and the derived class
constructor sample is used to initialize the data members defined in derived class.

POLYMORPHISM
Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives
different meanings or functions to the operators or functions. Poly, referring to many, signifies the many
uses of these operators and functions. A single function usage or an operator functioning in many ways
can be called polymorphism. Polymorphism refers to codes, operations or objects that behave
differently in different contexts.

Below is a simple example of the above concept of polymorphism:


6 + 10
The above refers to integer addition.

The same + operator can be used with different meanings with strings:
"Shrey" + "ankur"

The same + operator can also be used for floating point addition:
7.15 + 3.78

Polymorphism is a powerful feature of the object oriented programming language C++. A single operator
+ behaves differently in different contexts such as integer, float or strings referring the concept of
polymorphism. The above concept leads to operator overloading. The concept of overloading is also a
branch of polymorphism. When the exiting operator or function operates on new data type it is
overloaded. This feature of polymorphism leads to the concept of virtual methods. 

Polymorphism refers to the ability to call different functions by using only one type of function call.  
Suppose a programmer wants to code vehicles of different shapes such as circles, squares, rectangles,
etc. One way to define each of these classes is to have a member function for each that makes vehicles
of each shape. Another convenient approach the programmer can take is to define a base class named
Shape and then create an instance of that class. The programmer can have array that hold pointers to all
different objects of the vehicle followed by a simple loop structure to make the vehicle, as per the shape
desired, by inserting pointers into the defined array. This approach leads to different functions executed

6
SHREYANKUR TRIPATHI
COMPUTER ASSIGNMENT

by the same function call. Polymorphism is used to give different meanings to the same concept. This is
the basis for Virtual function implementation.

In polymorphism, a single function or an operator functioning in many ways depends upon the usage to
function properly. In order for this to occur, the following conditions must apply:

 All different classes must be derived from a single base class. In the above example, the shapes
of vehicles (circle, triangle, rectangle) are from the single base class called Shape.
 The member function must be declared virtual in the base class. In the above example, the
member function for making the vehicle should be made as virtual to the base class.

7
SHREYANKUR TRIPATHI

You might also like