Mid-Sem Model Solution 3
Mid-Sem Model Solution 3
self type?
Yes for static object of self type, it can also have pointer to self type
and No for non-static object of self type.
i.e., A class declaration can contain static object of self type, it can
also have pointer to self type, but it cannot have a non-static object of
self type.
// A class can have a static member of self type
#include<iostream>
class Test {
static Test self; // works fine
};
int main()
{
Test t;
getchar();
return 0;
}
class Test {
Test * self; //works fine
};
int main()
{
Test t;
getchar();
return 0;
}
// A class cannot have non-static object(s) of self type.
#include<iostream>
class Test {
Test self; // Error
};
int main()
{
Test t;
getchar();
return 0;
}
1(b)
#include <iostream>
#include <string>
using namespace std;
class Date {
private:
int day, month, year;
public:
Date(int d, int m, int y) {
day = d;
month = m;
year = y;
}
Date(string dateStr) {
// Parse the string in the format "*dd-mmm-yyyy*"
string dayStr = dateStr.substr(1, 2);
day = stoi(dayStr);
string monthStr = dateStr.substr(4, 3);
string months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"};
for (int i = 0; i < 12; i++) {
if (monthStr == months[i]) {
month = i+1;
break;
}
}
string yearStr = dateStr.substr(8);
year = stoi(yearStr);
}
void printDate() {
cout << day << "-" << month << "-" << year << endl;
}
};
int main() {
Date d1(22, 9, 2007);
Date d2("*22-Sep-2007*");
d1.printDate();
d2.printDate();
return 0;
}
Question 2 – Model Solutions
(c) (2 M)
If a class data member is declared as static, any copy of that member is created
regardless of the number of objects.
All the objects of a class share the single copy of the static data member.
Unlike non-static members, the static members are associated with the class as whole,
rather than with individual objects, however, are accessible by any of them.
Characteristics: (2 M)
3. (a) What is a friend function? Can a member function of one class become friend function
of another class? Explain with example.
[(1+3)=4 M]
(b) Write a complete C++ program to explain a scenario where we would like to use friend
function rather than member function for overloading binary operator.
[7 M]
(c) A class XYZ has a constructor as follows:
XYZ(int x, float y);
Can we use this constructor to convert type? Justify your answer.
Suppose we have two classes X and Y. If x is an object of X and y is an object of Y and we want
to use statement like x=y; then what type of conversion routine should be used and where?
[(2+2)=4 M]
=================================================================
Answer
3 (a)
A friend function is a function which is declared (either in the private or public or protected
part of a class) with a preceding keyword friend.
A friend function, although not a member function (and thus not in the scope of the class), has
full access rights to all the members (including public, private and protected members) of the
class to which it has been declared as a friend.
Yes, a member function of one class can become friend function of another class. In this case,
we need to specify the scope of the function before the function name, at the time of declaration
as a friend in the other class.
Example:-
class A {
int m;
public:
void func1();
void func2(int);
};
class B {
int x;
public:
int getfunc(int);
void showfunc();
friend void A::func1();
};
In the above example, func1()is a member function of class A, which has been declared as
a friend function for the class B.
[If the class A is declared as a friend of class B, then all the member functions of class A would
become friend functions of class B.]
3 (b)
When we need to use two different types of operands, namely an object (class type) and another
built-in type operand for a binary operator, then we must use friend function for overloading
the operator.
For example, if A and B are two vector objects, and the * operator is overloaded using member
function, then
A = B * 2; is valid
But
A = 2 * B; will not work.
Because the left hand operand is responsible for invoking the member function and this cannot
be done in the second case. In this scenario, friend function can be used for overloading the *
operator to allow both the cases as mentioned above.
Code:-
#include <iostream>
using namespace std;
class vector{
int v[size];
public:
vector(); // constructs null vector
vector(int *); // constructs vector from array
void display(){
cout<<"[ ";
for (int i=0;i<size;i++)
cout<<v[i]<<" ";
cout<<"]\n";
}
friend vector operator * (int a, vector b);
friend vector operator * (vector b, int a);
};
vector:: vector ()
{
for (int i=0;i<size;i++)
v[i]=0;
}
vector:: vector (int * x)
{
for (int i=0;i<size;i++)
v[i]=x[i];
}
vector operator * (int a, vector b)
{
vector c;
for (int i=0;i<size;i++)
c.v[i]=a*b.v[i];
return c;
}
vector operator * (vector b, int a)
{
vector c;
for (int i=0;i<size;i++)
c.v[i]=b.v[i]*a;
return c;
}
int x[size]={3,5,7};
int main ()
{
vector m;
vector n=x;
cout<<"m=";
m.display();
cout<<"n=";
n.display();
cout<<"\n";
vector p,q,r;
p= 2*n; //will not work for member function
q= n*2;
r= 5*m; //will not work for member function
cout<<"p="; p.display();
cout<<"q="; q.display();
cout<<"r="; r.display();
return 0;
}
3 (c)
No.
The constructors used for type conversion take single argument whose type is to be converted.
Here, the given constructor is taking two arguments, and so, cannot be used for type conversion.
Y:: operator X( )
{
X x;
/*
function body
*/
return x;
}
Answer 4
For Part a) and b), full marks will be awarded only if you have indicated the errors men:oned
below.
4 a). The class does not have a parameterised constructor accep:ng a single integer as
parameter. Thus, the crea:on of C2 and C3 objects will result in compila:on/syntax errors.
4 b). The Associa:on() method is declared as protected. An aHempt to access the same in
main() will result in a compila:on/syntax error.
Bank_Account.h
1 #ifndef __Bank_Account
2 #define __Bank_Account
3
4 #include<string>
5
6 using std::string;
7
8 namespace exam
9 {
10 class Bank_Account
11 {
12 private:
13 string customer_name;
14 string account_number;
15 long current_balance;
16 bool savings_account;
17
18 public:
19 // Parameterised Constructor
20 Bank_Account(string, string, long, bool);
21 // Method for depositing money
22 void deposit(long);
23 // Method for withdrawing money
24 bool withdraw(long);
25 // Method for displaying customer details
26 void display();
27 };
28 }
29 #endif
30
Bank_Account.cpp
Bank_Account_Example.cpp
Output