SRN
PES University, Bangalore UE18CS208B
(Established under Karnataka Act No. 16 of 2013)
Special Topic ESA
END SEMESTER ASSESSMENT (ESA) III SEMESTER- November 2019
UE18CS208B- Programming with C++
Time: 2 Hrs Answer All Questions Max Marks: 50
1. a) Write the steps in Overload Resolution.
Apply Overload Resolution, to the following 8 functions:
In the context of a list of function prototypes:
int g(double); // F1 void f(); // F2
void f(int); // F3 double h(void); // F4
int g(char, int); // F5 void f(double, double = 3.4); // F6 4M
void h(int, double); // F7 void f(char, char *); // F8
The function call to resolve is:
f(5.6);
Note: Write the answer for every step.
b) Write the output of the program given below:
#include <iostream>
using namespace std;
int wd=30;
char ch='/';
int ht( );
void f2( );
void defaultCheck(int=wd,int=ht( ),char=ch);
int main( ) {
defaultCheck();
f2( );
return 0; 2M
}
int ht( ) {return 30;}
void f2( ) {
int wd = 35;
ch = '*';
defaultCheck();
}
void defaultCheck(int wd,int ht,char ch) {
cout<<"wd= "<<wd<<" ht= "<<ht<<" ch= "<<ch<<endl;
}
SRN
c) Fill in the blank at LINE-1 & LINE-2 in the below program such that output would be: 10,12
#include <iostream>
using namespace std;
int incr(__________ int &x) { // LINE-1
{
2M
return __________; // LINE-2
}
int main( ) {
int x = 10; int y = incr(x + 1); cout << x << "," << y<<endl; return 0;
}
d) What is a Lambda Function? Explain its syntax. 2M
2. a) Define class, object. Explain the different types of access specifiers in C++. 2M
b) Why the parameter to a copy constructor must be passed as Call-by-Reference? 2M
c) Write the copy constructor, copy assignment operator and destructor for the class given
below.
class String
{
char *str;
int len;
public:
6M
String(char *s)
{
str=new char[strlen(s)+1];
strcpy(str,s);
len=strlen(str);
}
};
SRN
3. a) Write the definitions for constructor and all methods given in the public section of the
Vector class to match the given output.
#include<iostream>
using namespace std;
class Vector
{
double x;
double y;
public:
Vector(double x1=0,double y1=0);
void display( );
Vector& operator++( );
Vector operator++(int);
Vector& operator-( );
};
int main( ) {
Vector v1(2,3); 5M
Vector v2(4,5);
Vector v3(6,7);
Vector res1=++v1; [Link]( ); [Link]( ); cout<<endl;
Vector res2=v2++; [Link]( ); [Link]( ); cout<<endl;
Vector res3=-v3; [Link]( ); [Link]( ); cout<<endl;
return 0;
}
Output:
(3,4)
(3,4)
(4,5)
(5,6)
(-6,-7)
(-6,-7)
SRN
b) Fill in the blank1 through blank3 in the below program to match the given input & output.
#include<iostream>
using namespace std;
class MyClass {
static int x;
public:
void get( ) { x++; }
_______ ________ print(int y) { //Fill in the blank1 and blank2 with proper keywords
x = x - y;
cout << x <<" "<<endl;
}
}; 2M
______________________; //Fill in the blank3
int main( ) {
int n;
cin >> n;
MyClass:: print(n);
MyClass o1;
[Link]( );
[Link](n);
return 0;
}
Input: 5 Output: -4 -8
c) Categorize all the statements given below into true or false statements.
A friend function of a class:
i) has access to the private and protected members of the class to which it is a friend 3M
ii) must have its prototype included within the scope of the class prefixed with the keyword friend
iii) is always called with an invoking object of the class
4. a) Define Inheritance in C++. Write the general form of Inheritance in C++. 2M
b) Write the output of the below program.
#include<iostream>
using namespace std;
class X {
public:
virtual void f( ) { cout<<"X::f( )"<<endl; }
void g( ) { cout<<"X::g( )"<<endl;}
};
class Y : public X {
public:
void f() { cout<<"Y::f( )"<<endl;}
2M
void g() { cout<<"Y::g( )"<<endl; }
};
int main( ) {
X x; Y y;
X* px = &y;
x.f();
x.g();
px->f( );
px->g( );
return 0;
}
SRN
c) Define the class Rectangle and class Circle which inherits the class Shape given below
such that it matches the output given below.
#include<iostream>
using namespace std;
class Shape
{
public:virtual double area( )=0;
}; 6M
int main()
{
Rectangle r(2,3); Circle c(2);
cout<<[Link]( )<<" "<<[Link]( )<<endl;
return 0;
}
Output: 6 12.568
5. a) Write the output of the below program.
#include<iostream>
using namespace std;
class A {
protected: int a;
public:
A(int a1){a=a1;cout<<"ctor A"<<endl;} A(){cout<<"default ctor A"<<endl;}
~A(){cout<<"dtor A"<<endl;}
};
class B:public A {
protected: int b;
public:
B(int a1,int b1):A(a1){b=b1;cout<<"ctor B"<<endl;}
~B(){cout<<"dtor B"<<endl;}
};
class C:public A {
protected: int c; 3M
public:
C(int a1,int c1):A(a1){c=c1;cout<<"ctor C"<<endl;}
~C(){cout<<"dtor C"<<endl;}
};
class D:public B,public C {
protected: int d;
public:
D(int a1,int b1,int c1,int d1):B(a1,b1),C(a1,c1){d=d1;cout<<"ctor D"<<endl;}
~D(){cout<<"dtor D"<<endl;}
void show(){cout<<"a="<<B::a<<" b="<<b<<" c="<<c<<" d="<<d<<endl;}
};
int main( ) {
D d(1,2,3,4);
[Link]( );
return 0;
}
b) Write a C++ program to illustrate the use of class templates. 3M
c) Write a C++ program to handle divide by zero exception. 4M