0% found this document useful (0 votes)
36 views8 pages

Practice Problem of Classes

The code defines a class A with data members a and b and static data members x and p. It defines different constructors for A. When various objects of A are created and initialized, their data members and static data members are accessed and printed accordingly. The output printed is 3, 1, 3, 2, 4.

Uploaded by

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

Practice Problem of Classes

The code defines a class A with data members a and b and static data members x and p. It defines different constructors for A. When various objects of A are created and initialized, their data members and static data members are accessed and printed accordingly. The output printed is 3, 1, 3, 2, 4.

Uploaded by

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

Question 1

a) What would be the output produced by executing the following C++ code? Identify errors, if any
(Either write output or error , Both will not be acceptable).

#include <iostream>
using namespace std;
class Number {
private:
int n;
public:
Number() : n(0) {
cout << n;
}

Number( int nn ): n(nn)


{
cout << n;
}

Number(Number const& otherNum): n(otherNum.n+1)


{
cout << n;
}

void display() { cout << n; }


void increase() { n += 1; }
};
int main(){
Number a, b(1), c(b);
b.increase();
c.display();
b.display();
}

01222

Page 1 of 8
b) What would be the output produced by executing the following C++ code? Identify errors, if any
(Either write output or error, both will not be acceptable).

#include <iostream>
using namespace std;
class Memory {
float capacity;
public:
Memory(int cap = 1) {
capacity = cap;
cout << " Added Memory of Capacity= "
<< capacity << " G " << endl;
}
~Memory() {
cout << " Removed Memory of Capacity= "
<< capacity << " G " << endl;
}
};
class Core {
float speed;
public:
Core(float speed_ = 3.3) {
speed = speed_;
cout << " Added 1 Core of Speed= "
<< speed << " GHz " << endl;
}
~Core() {
cout << " Removed 1 Core of Speed= "
<< speed << " GHz " << endl;
}
};
class Processor {
const int ncores;
Core cores[4];
public:
Processor() :
ncores(4) {
cout << " Added a Processor of "
<< ncores << " Cores " << endl;
}
~Processor() {
cout << " Removed a Processor of = "
<< ncores << " cores " << endl;
}
};
class Mobile {
Memory m;
Processor p;
public:
Mobile() {
cout << " Building a Mobile " << endl;
Page 2 of 8
}
~Mobile() {
cout << " Destroying a Mobile " << endl;
}
};
int main() {
Mobile m; cout << " :) The End " << endl; }

Added Memory of Capacity= 1 G


Added 1 Core of Speed= 3.3 GHz
Added 1 Core of Speed= 3.3 GHz
Added 1 Core of Speed= 3.3 GHz
Added 1 Core of Speed= 3.3 GHz
Added a Processor of 4 Cores
Building a Mobile
:) The End
Destroying a Mobile
Removed a Processor of = 4 cores
Removed 1 Core of Speed= 3.3 GHz
Removed 1 Core of Speed= 3.3 GHz
Removed 1 Core of Speed= 3.3 GHz
Removed 1 Core of Speed= 3.3 GHz
Removed Memory of Capacity= 1 G

Page 3 of 8
c) What would be the output produced by executing the following C++ code? Identify errors, if any
(Either write output or error, both will not be acceptable).

#include <iostream>
#include <cassert>
using namespace std;
class Number {
public:
static int n;
Number() {cout << n++<<endl; }

Number(int i) {n=i;cout << n<<endl;}


static void somefunc(){ n=5;}

Number(Number const& otherNum){ cout << n<<endl; }

~Number(){cout<<--n;}
};

void fun(Number n){


cout<<n.n<<endl;
n.somefunc();
}
int Number::n=0;

int main(){
Number a, b(9), c(a);
fun(b);

return 0;
}
0
9
9
9
9
4321

Page 4 of 8
d) What would be the output produced by executing the following C++ code? Identify errors, if any
(Either write output or error, both will not be acceptable).

#include <iostream>
using namespace std;
class Integer {

private: int *n;

public:
Integer() : n(new int) { *n=5;}

Integer( int nn ):n(new int){ *n=nn; cout << *n<<" "; }

Integer(Integer const& otherNum): n(otherNum.n){ cout << *n<<" "; *n+=4; }

void display() { cout << *n<<" "; }

void increase() { *n += 1; } };

int main(){

Integer a, b(1), c(b);

b.increase();

c.display();

b.display(); }
1 1 6 6

Page 5 of 8
#include <iostream>
using namespace std; A::A(A &temp)
{
class A{ p= new int;
int *p; *p = *(temp.p);
static int a; }

public: void A::display()


A(); {
A(int); cout << *(this->p) <<endl;
A(A&);
void display(); }
void setter(int);
void A::setter(int k)
}; {
*(this->p) = k;
int A::a = 7;
}
A::A()
{ int main() {
p= new int (a++); A obj1;
(*p)++; A obj2 = obj1;
} obj1.display();
obj2.display();
A::A(int a)
{ obj2.setter(12);
p= new int; obj1.display();
*p = a; obj2.display();
} return 0;
}

8
8
8
12

#include <iostream>
using namespace std; A::A(A &temp)
{
class A{ p= new int;
int *p; *p = *(temp.p);
static int a; }

public: void A::display()


A(); {
A(int); cout << *(this->p) <<endl;
A(A&);
void display(); }
Page 6 of 8
void setter(int);
void A::setter(int k)
}; {
*(this->p) = k;
int A::a = 7;
}
A::A() int main() {
{ A obj1;
p= new int (a++); A obj2 = obj1;
(*p)++; obj2.setter(12);
}
A* obj3 = new A(obj2);
A::A(int a) obj3.setter(15);
{ obj2 = obj3;
p= new int; obj1.display();
*p = a; obj2.display();
} obj3.display();
return 0;
}
Error1 : obj3 is a pointer, cannot call setter function with . operator.
Either call it with arrow notation obj3->setter(15) or with deference
notation (*obj3).setter(15).

Error2: Cannot assign pointer to object. Obj2 is an object and Obj3 is a


pointer.

Error3: obj3 is a pointer, cannot call display function with . operator.


Either call it with arrow notation obj3->display() or with deference
notation (*obj3). display()

Page 7 of 8
#include <iostream>
using namespace std; A::A(int a)
{
int global = 9; this->a = a;
(this->a)++;
class A{
int a,b; b = this->a;
static int x; x--;
static int* p; cout << a <<endl;
p = &(this->a);
public:
A(); }
A(int);
A(int, int); A::A(int a, int b)
}; {
this->a = a;
int A::x = 19; this->b = 2;
int* A::p = &global; cout << a << endl;
x++;
A::A() cout << this->b << endl;
{ cout << *p;
cout << 1<<endl;
x+5; }
} int main() {
A obj1(3),obj4;
A obj2 = obj1;

A* obj3 = new A(3,5);


return 0;
}
3
1
3
2
4

Page 8 of 8

You might also like