0% found this document useful (0 votes)
18 views12 pages

Mid-Sem Model Solution 3

Uploaded by

Mohit Gupta
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)
18 views12 pages

Mid-Sem Model Solution 3

Uploaded by

Mohit Gupta
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/ 12

Can a C++ class have an object of

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>

using namespace std;

class Test {
static Test self; // works fine

/* other stuff in class*/

};

int main()
{
Test t;
getchar();
return 0;
}

// A class can have a pointer to self type


#include<iostream>

using namespace std;

class Test {
Test * self; //works fine

/* other stuff in class*/

};

int main()
{
Test t;
getchar();
return 0;
}
// A class cannot have non-static object(s) of self type.
#include<iostream>

using namespace std;

class Test {
Test self; // Error

/* other stuff in class*/

};

int main()
{
Test t;
getchar();
return 0;
}

If a non-static object is member then declaration of class is incomplete


and compiler has no way to find out size of the objects of the class.
Static variables do not contribute to the size of objects. So no problem
in calculating size with static variables of self type.
For a compiler, all pointers have a fixed size irrespective of the data
type they are pointing to, so no problem with this also.

1(b)

/* C++ Program to Display Date using Constructors */

#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

2 (a) float Multiply (float m, int n) (2 M)


{ float result =m, i;
for (i=0; i<n; i++)
{ result = result*m; } return result; }

int Multiply (int m , int n) (2 M)


{ float result =m, i;
for (i=0; i<n; i++)
{ result = result*m; } return result; }

(b) Output: BACDBACE~E~C~A~B~D~C~A~B (4 M)

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

 It is initiated to zero when the first objet of it’s class is created.


 Only one copy of that member is created for the entire class and shared by all the
objects.
 It is visible only with in the class, but life time is the entire program.
 These are normally used to maintain values common to the entire class.
==================================================================
Question

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;

const int size = 3;

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.

We have to use one class to another class type conversion.


This can be carried out by using either a constructor or a conversion function. Which form to
use, depends upon whether the type conversion is to be done in the source class or in the
destination class. Following is a summary of the same.

Conversion Required Conversion takes place in


Source Class Destination class
Class  Class Using Using Constructor
Conversion function/
Casting Operator

Here, Y is the source class and X is the destination class.


Thus, the conversion routines can be as follows:
i) // using Constructor in class X
-----------------------------------
class X
{
/*member declaration*/
public:
X (Y y)
{
/*constructor body*/
}
/*member declaration*/
}

ii) // using Conversion function/ overloaded Casting Operator in class Y


------------------------------------------------------------------------------------

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.

For Part c), below is one possible solu:on.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy