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

FMT Engineer

Fmt

Uploaded by

verkoyenjosef14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

FMT Engineer

Fmt

Uploaded by

verkoyenjosef14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Faculty of Engineering & Architecture

Object Oriented Programming (SOFE 204)


ASSIGNMENT#3(5%)
Instructor: Umut Zeki Deadline: 18.01.2024

1. Explanation questions. Answer the following with a few sentences.


(Use your own words. Also, your explanation should be paralllel with the lecture resources.
Otherwise your answer will not be accepted.)
a. Give a real life example that explains data abstraction.
b. Data abstraction refers to …
c. What is the relationship between predefined functions and the abstraction?
d. Write two important advantages of data abstraction.
e. Write the designing strategy of abstraction.
f. What is the difference between data abstraction and encapsulation.
g. What are the negative effects of making one class a friend of another in terms of
encapsulation?
h. What is the difference between Overloading and Overriding?
i. What is the difference between single and multiple inheritance?
j. What is the difference between class and object?
k. What is the difference between a member and friend function?
l. What is the main purpose of C++ programming?
m. What is the function of inheritance in C++?
n. What is based and derived class?
o. There are some exceptions that a derived class not inherits some base class methods,
what are they?
p. Draw a table to summarize the different access types according to who can access them.
q. Explain public inheritance rules.
r. What is a class constructor?
s. Why constructors are useful?
t. What is a parameterized constructor?
u. Check the program related with parameterized constructor.
v. What is a destructor?
w. Why we need to use destructors?
Faculty of Engineering & Architecture
2. Trace questions. Trace the following questions and write the outputs and the functions
(what the program does) of the programs.
a. #include<iostream> OUTPUT
using namespace std;
class BaseClass1 {
public:
BaseClass1()
{ cout << " BaseClass1 constructor called" << endl; }
};
class BaseClass2 {
public:
BaseClass2()
{ cout << "BaseClass2 constructor called" << endl; }
};
class DerivedClass: public BaseClass1, public BaseClass2 { FUNCTION
public:
DerivedClass()
{ cout << "DerivedClass constructor called" << endl; }
};
int main()
{
DerivedClass derived_class;
return 0; }

b. using namespace std;


OUTPUT
class A {
public:
void print()
{ cout <<" Inside A::"; }
};
class B : public A {
public:
void print()
{ cout <<" Inside B"; }
};
class C: public B {
};
FUNCTION
int main(void)
{
C c;

c.print();
return 0;
}
Faculty of Engineering & Architecture
c. class Box {
public: OUTPUT
static int objectCount;
Box(double l = 2.0, double b = 2.0,
double h = 2.0) {
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
objectCount++; }
double Volume() {
return length * breadth * height; }
private: FUNCTION
double length;
double breadth;
double height; };
int Box::objectCount = 0;
int main(void) {
Box Box1(3.3, 1.2, 1.5);
Box Box2(8.5, 6.0, 2.0);
cout << "Total objects: " << Box::objectCount << endl;
return 0; }

d.

int main() {
class Animal { Animal myAnimal;
public: Pig myPig;
void animalSound() { Dog myDog;
cout << "The animal makes a sound \n" ; myAnimal.animalSound();
} }; myPig.animalSound();
class Pig : public Animal { myDog.animalSound();
public: return 0;
void animalSound() {
cout << "The pig says: wee wee \n" ; OUTPUT
} };
class Dog : public Animal {
public:
void animalSound() {
cout << "The dog says: bow wow \n" ;
} };
Faculty of Engineering & Architecture
3. Coding Questions.

a. Const Co. is a construction company that one of the services is surrounding the gardens
with bricks. The boss Mr. Brickstone ask to the programming department of the company
to write a program in C++ to calculate the cost of any garden by checking the garden
shape(area of triangle or rectangle) and material type (fence). Fence cost is 200TL/m.

Programming Steps:
 You have to use multiple inheritance to solve this problem. There should be two base
classes as shape and material and two derived classes from these base classes as triangle
and rectangle.
 You should use a constructor to initialize the brick cost price.
 Also you have to put a static variable to count and print how many times this program is
executed at the end.
 Using formulas, setter and getter functions and inheritance methods should be similar to
the multiple inheritance example.

Program output will be as follows:


Enter the garden shape: SAMPLE PROGRAM
(t)riangle
class Shape {
(r)ectangle
public:
Write first letter to select: r
void setWidth(int w) {
Enter the width in meters: 15.0
width = w; }
Enter the lenght in meters: 10.0
void setHeight(int h) {
Area: 150.0 meters
height = h; }
Cost(TL): 75000.0 TL
protected:
Do you want to continue? (Y)es or (N)o: y
int width;
int height; };
Enter the garden shape:
(t)riangle class Rectangle: public Shape {
(r)ectangle public:
Write first letter to select: t int getArea() {
Enter the width in meters: 10.0 return (width * height);
Enter the lenght in meters: 10.0 } };
Area: 50.0 meters
Cost(TL): 25000.0 TL int main(void) {
Do you want to continue? (Y)es or (N)o: n Rectangle Rect;
This program executed 2 times. Rect.setWidth(5);
Rect.setHeight(7);
cout << "Total area: " <<
Rect.getArea() << endl;
return 0;
}
Faculty of Engineering & Architecture
b. Modify the following program to take the width and heigth as user input.
Program should prompt again, while user presses the character ‘c’ or ‘C’(use do-while).
Add a triangle and pentagon class to the program.
Use switch-case to select which shape it will be used to calculate the area and the paint
cost.

#include <iostream>

using namespace std;


Sample Output:
// Base class Shape
class Shape { Enter width: 5
public:
void setWidth(int w) { Enter height: 8
width = w;
Enter paint cost: 30
}
void setHeight(int h) {
Select triangle,rectangle or
height = h;
} pentagon(press first
character to select): t
protected:
int width; Total Area: 20
int height; Total paint cost: 160
};
Press ‘C’ to continue… c
// Base class PaintCost
class PaintCost { Enter width: 5
public: …
int getCost(int area) {
return area * 70;
}
};

// Derived class
class Rectangle: public Shape, public PaintCost {
public:
int getArea() {
return (width * height);
}
};

int main(void) {
Rectangle Rect;
int area;

Rect.setWidth(5);
Rect.setHeight(7);

area = Rect.getArea();
Faculty of Engineering & Architecture

// Print the area of the object.


cout << "Total area: " << Rect.getArea() << endl;

// Print the total cost of painting


cout << "Total paint cost: $" << Rect.getCost(area) << endl;

return 0;
}

c. Write a program and use function overloading to give the following output:

Output:
Operation type (Addition or Multiplication): +
Number of operands (2 or 3): 2
Enter no1: 5
Enter not2:6
Result=30

Overloading function prototypes for addition and multiplication:


int add(int a, int b)
int add(int a, int b, int c)
float add(int a, int b)
float add(int a, int b, int c)

int mult(int a, int b)


int mult(int a, int b, int c)
float mult(int a, int b)
float mult(int a, int b, int c)

d. Assume that you are working in a company as a programmer that laying parquet to
houses. Manager asked you to write a program to calculate the cost of laying parquet in
a house. The program should take the area of the house room by room.
Eg. Room1: 25m2
Room2: 30m2
+ Room3: 40m2
________________________

Total=95 m2

After calculating the total area, the program should multiply it to the unit(m2) price of the
parquet.
Eg. Total=95 m2
Parquet(m2)=200TL/ m2
Faculty of Engineering & Architecture
Cost= 95x200=19000TL

The output of the program should be as follows:


Enter room1 area: 25
More rooms?(Y/N): Y
Enter room2 area:30
More rooms?(Y/N): Y
Enter room3 area:40
More rooms?(Y/N): N
Enter the unit price/m2:200
Total Area=95 m2
Cost=19000

You might also like