0% found this document useful (0 votes)
8 views6 pages

MC 307 Assignment-1

The document is an assignment for an Object Oriented Programming course, containing multiple programming questions that require identifying errors in given C++ code snippets, explaining concepts like friend functions, composition vs inheritance, operator overloading, and polymorphism. Each question involves analyzing code, providing outputs or explanations, and demonstrating understanding of OOP principles. The assignment covers various aspects of C++ programming including class construction, destructors, and function overloading.

Uploaded by

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

MC 307 Assignment-1

The document is an assignment for an Object Oriented Programming course, containing multiple programming questions that require identifying errors in given C++ code snippets, explaining concepts like friend functions, composition vs inheritance, operator overloading, and polymorphism. Each question involves analyzing code, providing outputs or explanations, and demonstrating understanding of OOP principles. The assignment covers various aspects of C++ programming including class construction, destructors, and function overloading.

Uploaded by

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

MC 307 OBJECT ORIENTED PROGRAMMING

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();
}

(b). class World


{ private: int a; int b;
public:
World(int x, int y)
{ a=x; b=y; }

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();
}

(c). class World


{ private: int a; int b;
public:
World(int = 0, int = 0);
void print();
};
World : : World(int x, int y)
{ a=x; b=y; }
void World : : print()
{ cout<<” object created with values ”<<a<<b; }
int main()
{
World obj1; World obj2(24); World obj3(45);
obj1.print();
obj2.print();
obj3.print();
}

(d). class World


{ private: int a; int b;
public:
World(int x, int y)
{ a=x; b=y;
cout<<”object created with values”<<a<<b; }
~ World( )
{ cout<<”object deleted with values”<<a<<b; }
};
World q(10, 20);
int main()
{
World w(24, 56);
static World r(45, 87);
{
World w(33, 22);
static World z(99, 66);
}
World d(50, 100);
}

(e). class Hello


{ private: int a;
public:
Hello (int z)
{ a=z;
print();
}
void print( ) { cout << a; }
void fun() const {cout<<”Function called”;}
};
int main( )
{
Hello obj1 (24);
const Hello obj2 (34);
obj1.print();
obj2.print();
obj1.fun();
obj2.fun();
}

(f) class Hello


{ private: int a;
public:
Hello ( )
{ cout<<”Hello object created”;
}
~Hello ( )
{ cout<<”Hello object destroyed”;
}
void fun(int z)
{ a=z; }
};

class World : public Hello


{
private: int b;
World ()
{ Hello();
}
void fun(int q, int w)
{ a=q; b=w; }
};
int main( )
{
Hello obj1(); World obj2();
obj1.fun(10);
obj2.fun(20, 30);
}

(g) class Hello


{ protected: int a;
public:
Hello ( )
{ cout<<”Hello object created”;
}
~Hello ( )
{ cout<<”Hello object destroyed”;
}
void fun(int z)
{ a=z; }
};

class World : public Hello


{
private: int b;
World () : Hello()
{ cout<<”World object created”;
}
~World ( )
{ cout<<”World object destroyed”;
}

void fun(int q, int w)


{ a=q; b=w; }
};
int main( )
{
Hello obj1(); World obj2();
obj1.fun(10);
obj2.fun(20, 30);
}

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.

You might also like