You are on page 1of 3

Assignment 10

Naeem Zainuddin 1845120

Q1: Demonstrate the use Object Oriented Programming?

Answer:

• Encapsulation: Objects created in OOPs are able to hide certain parts of code from programmer.
This prevents unintentional modification in the code which may cause unwanted outcomes.

• Code Reuse: Objects created in OOPs can easily be reused in other programs.

• Software Maintenance: Code written in OOPs is easy to debug and maintain.

• Design Benefits: OOPs needs extensive design planning which certainly provide design benefits
over traditional style

Q2: Difference between employing functions and classes in programming.

Answer

Classes (or rather their instances) are for representing things. Classes are used to define the operations
supported by a particular class of objects (its instances). If your application needs to keep track of
people, then Person is probably a class; the instances of this class represent particular people you are
tracking.

Functions are for calculating things. They receive inputs and produce an output and/or have effects
Functions are easy to understand even for someone without any programming experience, but with a
fair math background. On the other hand, classes seem to be more difficult to grasp.

Q3: Define instantiation in object oriented programming?

Answer:

In programming, instantiation is the creation of a real instance or particular realization of an abstraction


or template such as a class of objects or a computer process. To instantiate is to create such an instance
by, for example, defining one particular variation of object within a class, giving it a name, and locating it
in some physical place. In object-oriented programming, some writers say that you instantiate a class to
create an object, a concrete instance of the class. The object is an executable file that you can run in a
computer.

Q4: Define the syntax of object programming?

Answer:
The basic syntax of defining a an object oriented Class :

class className{
// variables
// constructors
// functions
}

 class is the keyword to declare a class.


 className is the name by which the class can be referenced.
 finite number of variables could be declared inside the class, which help to represent the
state of objects belonging to the class type.
 constructors help to create objects of the class type with specified state passed as
arguments to them.
 finite number of functions could be declared and defined inside the class, which help to
define the behavior of objects of the class ty

Q5: Using Object oriented programming, write a program to calculate perimeter, area and
volume of a rectangle?

Answer:
#include "pch.h"
#include <iostream>

using namespace std;

class rectangle
{
int length;
int breadth;
int height;
public:
int getarea(int l, int b)
{
int area;
area = l * b;
cout << "Area = " << area << "\n";
return area;
}

int getperimeter(int l, int b)


{
int peri;
peri = 2 * (l + b);
cout << "Perimeter = " << peri << "\n";
return peri;
}
int getvolume(int l, int b, int h)
{
int volume;
volume = l * b * h;
cout << "Volume = " << volume << "\n";
return volume;
}

};
int main()
{
int l, b, h;
rectangle r;
cout << "Enter length of rectangle ";
cin >> l;
cout << "Enter breadth of rectangle ";
cin >> b;
cout << "Enter height of rectangle ";
cin >> h;
r.getarea(l, b);
r.getperimeter(l, b);
r.getvolume(l, b, h);

system("pause");

return 0;
}

You might also like