Você está na página 1de 3

Programming Techniques Sheet #4

CMP 103 & CMP N103 1/3 Spring 2014




1. Decide the relation (aggregation, association, or inheritance) between the classes in the
following cases
a. Point, Triangle, Quadrilateral, Parallelogram, and Rectangle.
b. Point, 3DPoint, 2DShape, 3DShape, Ellipse, Circle, Sphere, and Cylinder.
c. Head, Body, Tail, Animal, Dog, AnimalHouse, DogHouse, Lion, and Food.
d. Student, StudyRoom, StaffMember, Class, Door, Board, Lab, Course, Instructor,
Professor, and Department.

2. What is the output of the following program?
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A"; }
~A() { cout << "~A"; }
};
class B
{
public:
B() { cout << "B"; }
~B() { cout << "~B"; }
};
class C
{
public:
C() { cout << "C"; }
~C() { cout << "~C"; }
private:
B b;
A a;
};
class D
{
public:
D() { cout << "D"; }
~D() { cout << "~D"; }
};
class E : public C
{
public:
E() { cout << "E"; }
~E() { cout << "~E"; }
private:
D d;
B b;
};
int main()
{
E e;
cout << endl;
return 0;
}

3.
a. What is the output of the following program?
#include <iostream>
using namespace std;

class Parallelogram
{public:
double L,W;
Parallelogram() { L=2; W=1; }
virtual void printPerimeter()
{cout<<"\nParallelogram Perimeter
="<<2*(L+W); }
};
class Rectangle: public Parallelogram
{public:
double L,W;
Rectangle (double l, double w)
{L=l; W=w;}
virtual void printPerimeter()
{ cout<<"\nRect Perimeter ="<<2*(L+W);}
};
void FigPerimeter(Parallelogram *P)
{
cout<<"\nFig Perimeter="<<2*(P->L+P->W);
P->printPerimeter();
}

int main()
{
Parallelogram *pg =new Parallelogram;
pg->L = 4;
pg->W = 2;
FigPerimeter(pg);

Rectangle *R= new Rectangle(7,5);
FigPerimeter(R);

return 0;
}
b. What is the problem with program output? How can it be solved ?
Cairo University
Faculty of Engineering
Computer Engineering Department

Programming Techniques
Sheet #4
Programming Techniques Sheet #4

CMP 103 & CMP N103 2/3 Spring 2014

4. Create a class Wheel that represents a car wheel.
a. The Wheel should have a weight and a diameter.
b. The Wheel should have a default constructor for initialization.
c. Provide a function PrintInfo() that prints the Wheel information.

5. Create a class Car that has a color, a number of doors, and a fuel level.
a. Each Car has 4 Wheels
b. Provide appropriate set and get functions.
c. Write a non-default constructor that sets the data members.
d. Provide a function PrintInfo() that prints the Car information.

6. Use the classes Car and Wheel to build a special type of cars called Van.
a. Van should have a cargo in addition to member data of the Car.
b. Van has 8 Wheels.
c. Decide whether to use Composition or Inheritance to build the Van from the classes Car
and Wheel.
d. Provide a non-default constructor that sets all the Van's data members.
e. Show the order of construction of the Van parts.
f. Provide a version of the function PrintInfo() to print all the Van's information.

7.
a. Which line in the following program will produce compilation error?
#include <iostream>
using namespace std;

class Figure
{protected: int ID;
public:
Figure(int id)
{ ID = id; }
virtual double getArea()
{return 0;}
};

class Circle:public Figure
{double x,y, radius;
//x,y are for circle center
public:
Circle(int id, double rx, double ry,
double rad)
{ ID=id; x=rx; y=ry; radius=rad; }

double getRad()
{ return radius; }

double getArea()
{
return 3.14*radius*radius;
}
};
class Rect:public Figure
{
double L,W; //Lenght, Width
public:
Rect(int id, double l, double w)
{ ID=id; L=l; W=w; }

virtual double getArea()
{
return L*W;
}
};

int main()
{
e!t 1(1" 4" 3)" 2(3" 20" 10);
#ir!le #(2" $"%" 10);
&igure &(10);

&igure &List'(={1" #" 2" &};
int n=3;
//Print areas of all Figures
for(int i=0; i)n; i**)
cout<<FList[i].getArea()<<endl;


//Wtite code that scans FList and prints:
//1- no. of Rects
//2- no. of Circles
//3- Raduis of each Circle
return 0;
}

b. After correcting compilation errors, what is the program output?
c. Is there a problem with the program output? If so, how to solve it?
d. Complete the main as instructed by the comments then write the program output.

Programming Techniques Sheet #4

CMP 103 & CMP N103 3/3 Spring 2014

For problems 8-11, you are required to design a game that has classes as described below. For
each class you have to decide how to implement it and whether it is an abstract class or a regular
class.

8. Class Animal to represent an animal
a. An animal has an age and a health.
b. Provide the following functions. Then for each function, decide whether it should be
virtual or not.
i. Function MakeSound() that makes a different sound for each animal.
ii. Function Eat() that increases the health of the animal.
c. Should this class be an abstract class?

9. Create a class PeacefulAnimal to represent the type of the animals that may be attacked by
aggressive animals
a. Provide a function GotAttacked() that decreases the health of the Animal
according to the attacker power.
b. Provide a function Escape() for the peaceful animal to escape from its attacker.
c. Should this class be an abstract class?

10. Create a class AggressiveAnimal to represent the type of the animals that can attack
PeacefulAnimals
a. Each AggressiveAnimal has an AttackPower.
b. Provide a function Attack(.) for the AggressiveAnimal to attack PeacefulAnimal
i. What type of parameter(s) should be passed to this function?
ii. What functions should be called by this function?
iii. The function should destroy the PeacefulAnimal if its health decreases to zero.
iv. Each aggressive animal in the game has its own way to attack its prey.
v. Should this function be virtual?
c. Should this class be an abstract class?

11. Use the classes created in problems 7, 8, and 9 to create the classes Lion, Rabbit, Gazelle,
Wolf, and Sheep.
a. Decide which class to inherit from.
b. Override the functions that should to be overridden.
c. Which functions needn't be overridden?

Você também pode gostar