0% found this document useful (0 votes)
11 views

OOP Assignment 2

Uploaded by

Robert Brown
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

OOP Assignment 2

Uploaded by

Robert Brown
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Object Oriented Programming

Assignment No. 2,3


Inheritane & Polymorphism
Due Date/Time 26th May, 2023 11:30 PM
Files to be submitted Documentation along with code file.
File Naming Roll_No_Assign_2,3.pdf
Roll_No_Assign_2,3.cpp
Note: Any assignment that is not according to format will be marked
as zero.
Coding Guides 1. Use of proper variable declaration/initialization according to the
naming conventions (camelCase, snake_case, PascalCase )
2. Use of proper function for each question.
Note: Marks will be deducted if not following the above guide line.
Submission Guide 1. Code along with documentations should be submitted on teams
line by due date/time.
Plagiarism Any kind of plagiarism will result in F grade in course
Weightage This assignment will be marked on CLOs, also it will be graded for
lab course.

Question # 01: Animal


In this task, we implement multiple inheritance. We create two classes, a ‘Mammal’ and a ‘Bird’ class.
Then we create another class which inherits both the classes, we name the third class ‘Organism’. Consider
including the following attributes and functionalities to your classes.
class Mammal
● void setMammalName(string);
● void showMammal();
class Bird
● void setBirdName(string);
● void showBird();
class Organism
● void setOrganismName(string);
● void showOrganism();
● char* getOrganismName();

Question # 1: 02: Shapes


In this task, we would try to find out areas of various shapes using inheritance. You need to create a class
name ‘Base’. Define two protected data members of float type for length and width – ‘Length or Base’ and
‘Width or Height’. Now you need to define constructor(s), setter(s) and a getter for the base class. Next,
you need to define three child classes, ‘Rectangle’, ‘Square’ and ‘Triangle’. Every class must contain a
constructor that invokes the parent class constructor whenever a child instance is created. Following the
same approach, create the particular getter, setter(s) and area functions for each of the class. Area of a class
must return a value accordingly. Use a suitable inheritance.
class Base
● Base(float, float){}
● void baseGetter() const{}
class Rectangle
● Rectangle(float, float);
● void rectangleGetter(){}
● void rectangleSetter(){}
● void rectangleSetter(float, float){}
● float areaOfRectangle(){}
class Square
● Square(float, float);
● void squareGetter(){}
● void squareSetter(){}
● void squareSetter(float, float){}
● float areaOfSquare(){}
class Triangle
● Triangle(float, float);
● void triangleGetter(){}
● void triangleSetter(){}
● void triangleSetter(float, float){}
● float areaOfTriangle(){}

Question # 03:
We want to calculate the total marks of each student of a class in Physics, Chemistry and
Mathematics and the average marks of the class. The number of students in the class are entered
by the user. Create a class named Marks with data members for roll number, name and marks.
Create three other classes inheriting the Marks class, namely Physics, Chemistry and
Mathematics, which are used to define marks in individual subject of each student. Roll number
of each student will be generated automatically.
Question # 04:
Consider a class Computer having Two fields (i.e. companyName, price) and A single function named
show() A class named Desktop inherits Computer class and adds fields representing color, monitor size,
and processor type and Override function named show() to display values of its all attributes A class named
Laptop inherits Computer class and adds fields representing color, size, weight, and processor type and
Override function named show() to display values of its all attributes In Main() instantiate objects of derived
classes to access respective show() functions to see the polymorphic behavior.
Question # 05:
Write a class Distance that holds distances or measurements expressed in feets and inches. This class has
two private data members:

● feet: An integer that holds the feet.


● inches: An integer that holds the inches.
● Write a constructor with default parameters that initializes each data member of the class. If inches
are greater than equal to 12 then they must be appropriately converted to corresponding feet.
● Generate appropriate getter-setter functions for the data members.
o void setFeet(int f) and int getFeet()const
o void setInches(int i) It should ensure proper conversion to feet.
o int getInches() const
● Define an operator ‘+’ that overloads the standard ‘+’ math operator and allows one Distance
object to be added to another. Distance operator+ (const Distance &obj).
● Define an operator - function that overloads the standard ‘-‘ math operator and allows subtracting
one Distance object from another. Distance operator-(const Distance &obj)
● Define an operator= function that overloads the = operator and assign one Distance object to
another. const Distance operator=(const Distance &obj)
Question # 06:
You are supposed to construct a class named Matrix that shall contains private data member

● matrix: integer type array of size 3 by 3;

Define a constructor that should have default parameters that can set all elements to 0. Moreover, define a
function called setMatrixValues(int matrixArray[3][3]) that can assign values of matrixArray (provided to
the function in argument) to the matrix array (data member of the class Matrix) of the class. Along with it,
define a function called displayMatrix() that can display all values of the matrix.

Moreover, you are instructed to construct a program that can perform operator overloading for the following
operators + (plus), - (minus), and == (equal).

Question # 07:
We want to create a class of Product that contains multiple private data members such as

● quantity: An integer that holds a count value.


● objCount: A static integer that holds that count of objects.
● serialNo: An integer that holds the serial number of objects of a specific product (assume the single
object of Product class).
● Define a constructor that can accept two arguments i.e., totalQuantityOfProduct and
serialNumberOfProduct; and assign it to the respective data members of the class. Moreover, the
static member be shall be initialized from the outside of the class with zero by using scope
resolution operator.
● Define operator = that add the value of quantity to the left hand operand. i.e. c2=c1 (the quantity of
object c2 shall be incremented with the quantity of c1).
● Define unary operator - that inverts the value of quantity for product class and should allow the
statements like c1 -= 4;

END

You might also like