You are on page 1of 2

Programming Techniques Sheet #2

CMP 103 & CMP N103 1/2 Spring 2014





1.
a. What are the problems with the following function?
int Sum(int x , int y =2 , int z)
{
return x+y+z;
}
b. Write a corrected version of the function Sum.
c. Write different ways to call the corrected function Sum. In each call, determine the return
value.

2. What are the problems with the following functions declarations?
int func (int x , double y );
double func (int a , double b );


3. Write overloaded versions of a function called Compare that can compare two integers, or two
arrays. The function returns 1 if first parameter is the greater, -1 if it is the smaller and returns 0
otherwise. Assume any reasonable logic to compare the two arrays.

4. Which of the following functions may cause ambiguity if it coexists with the function declared in
the first line?
int func (int x , int y , int z =3); //first line

int func ();
int func (int a);
int func (int a, int b);


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

void f1(int a,int b)
{ cout<<endl<<a+b; }

void f2(int a,int &b)
{ cout<<endl<<a+b; }
int main()
{
int i=3;
f1(i*=2, ++i);
i=3;
//f2(i*=2, ++i);
return 0;
}
b. Uncomment f2 call and assume right-to-left argument evaluation order, what will be the
output.


Cairo University
Faculty of Engineering
Computer Engineering Department

Programming Techniques
Sheet #2
Programming Techniques Sheet #2

CMP 103 & CMP N103 2/2 Spring 2014

6. Create a class Rectangle with attributes length and width, with default values 2 and 1
respectively. Provide member functions that
a. Set (with verification) and get length and width.
b. Calculate the perimeter and the area of the rectangle.

7. What are the problems with the following class declaration?
a.

















8. Create a class called Point that represents a point (x, y) in the space.
a. Use double variables to represent the point coordinates. (private).
b. The Point constructor should take two arguments to initialize x and y.
c. Provide a public function Display that prints the point in the form (x, y).
d. Show how to declare an instance of the class Point.

9. Using the class Point, create a class Line that has two ending points P1 and P2.
a. P1 and P2 are private members.
b. The Line constructor should take 4 arguments and initialize P1 and P2.
c. Write a member method Display that prints the line's ending points.

10. Create a class called Complex for performing arithmetic with complex numbers.
Complex numbers have the form RealPart + ImaginaryPart * i (where i = 1 )
a. Use double variables to represent private data of the class.
b. Provide the following constructors that enable an object of this class to be initialized.
i. Default constructor (with no arguments).
ii. Constructor with arguments.
c. Provide public member functions for each of the following:
i. Setting and getting the values of real and imaginary parts.
ii. Printing the Complex number in the form (RealPart, ImaginaryPart)
iii. Adding another Complex number to the current number. The real parts are
added together and the imaginary parts are added together.

11. Write a program that
a. Defines two instances C1 and C2 of the class Complex.
b. Initializes C1 using default constructor.
c. Initializes C2 using constructor with arguments.
d. Prints C1 and C2.
e. Performs the addition operation C1 = C1 + C2.
f. Prints C1 and C2 after addition.

class Point
{
private:
float x = 0;
float y = 0;
public:
Point();
Point(float a=1, float b=1);
int Point(float m, float n);
void ~Point(float a, float b);
};

You might also like