MC 307 Assignment-1
MC 307 Assignment-1
ASSIGNMENT-1
2023-24 SESSION
Q.1. Find the error(s), if any, and explain the reason for the error(s) in the given programs. If
there is no error then write the output of the given programs. Assume that all the header files
have been incorporated in the programs wherever necessary.
a. class Hello
{ private: int a;
public:
Hello (int z)
{ a=z;
cout<<”An object created”;
}
void print( ) { cout << a; }
};
int main( )
{
Hello obj (24); Hello *ptr = &obj; Hello &ref = obj;
obj.print();
ref.print();
ptr.print();
}
void print()
{ cout<<” object created with values ”<<a<<b; }
};
int main()
{
World obj1; World obj2(24); World obj3(45);
obj1.print();
obj2.print();
obj3.print();
}
Q.2. Explain the concept of friend functions in C++ with the help of a program.
Q.3. Differentiate between “Composition” and “Inheritance” in C++. Explain with the help of
corresponding programs.
Q.4. Given a class “Complex” representing complex numbers with data members; one for real
part and other for imaginary part. Overload the following operators for the objects of class
Complex:
(a) Binary Addition: it should separately add the real parts and imaginary parts of two objects.
Overload with the help of a friend function.
(b) Pre Increment: it should increment the real part of a complex number by one. Overload
with the help of a member function.
Q.5. Explain the concept of polymorphism using an inheritance hierarchy. Say, you have a
base class named “Shape”. You can derive various classes from this base class such as “Circle”,
“Square”, “Rectangle”, etc. The base class should contain a function named “area”. Override
this function in the derived classes and show the polymorphic behaviour with the base class
pointer pointing to derived class objects.