CS103 Computer Programming Sessional II: National University of Computer and Emerging Sciences
CS103 Computer Programming Sessional II: National University of Computer and Emerging Sciences
CS103 Computer Programming Sessional II: National University of Computer and Emerging Sciences
I II III Total
Total Marks 40 20 40 100
Marks Obtained
21 int main() {
22 GuessMe g1;
23 g1.SetX(10);
24
25 GuessMe g2(g1);
26 cout << g2.GetX() << endl;
27 return 0;
28 }
(2) (5 Marks) What would be the output produced by executing the following C++ code? Identify and correct
errors, if any.
1 #include <iostream>
2 using namespace std;
3 class A {
4 public:
5 virtual void print() {
6 cout << "A" << endl;
7 }
8 };
9
10 class B: public A {
11 };
Page 2 of 11 Continue. . .
CS103 - Computer Programming Spring 2014 Sessional II
12
13 class C: public B {
14 public:
15 void print() {
16 cout << "C" << endl;
17 }
18 };
19
20 int main() {
21 C c;
22 B *b = &c;
23 b->print();
24 return 0;
25 }
(3) (10 Marks) The following C++ code has error(s) and you are not allowed to modify:
• Class A
• Display function in class B
What would you modify in class B to remove the error(s) such that the following output is produced:
num=5
gum=10
1 #include<iostream>
2 using namespace std;
3 class A {
4 private:
5 int num;
6 public:
7 A(int x = 0) :
8 num(x) {
9 }
10 A(const A& a) :
11 num(a.num) {
12 }
13 void Display() {
14 cout << "num=" << num << endl;
15 }
16 void SetNum(int x) {
17 num = x;
18 }
19 };
20
21 class B: public A {
22 private:
23 int gum;
24 public:
25 B(int x = 0, int y = 0) :
26 A(x), gum(y) {
27 }
Page 3 of 11 Continue. . .
CS103 - Computer Programming Spring 2014 Sessional II
28 B(const B& b) {
29 num = b.num;
30 gum = b.gum;
31 }
32 void Display() {
33 A::Display();
34 cout << "gum=" << gum << endl;
35 }
36 };
37
38 int main() {
39 B b1(5, 10);
40 B b2(b1);
41 b2.Display();
42 return 0;
43 }
(4) (5 Marks) What would be the output produced by executing the following C++ code? Identify and correct
errors, if any.
1 #include<iostream>
2 using namespace std;
3 class A {
4 int x;
5 public:
6 A(int val = 0) :
7 x(val) {
8 cout << "A " << x << endl << flush;
9 }
10 A(const A& a) {
11 x = a.x;
12 cout << "B " << x << endl << flush;
13 }
14 void SetX(int x) {
15 this->x = x;
16 }
17 ˜A() {
18 cout << "D " << x << endl << flush;
19 }
20 };
21
22 A f(A a) {
23 cout << " C " << endl << flush;
24 a.SetX(100);
25 return a;
26 }
27
Page 4 of 11 Continue. . .
CS103 - Computer Programming Spring 2014 Sessional II
28 int main() {
29 A a(1);
30 A b=f(a);
31 b.SetX(-100);
32 return 0;
33 }
(5) (15 Marks) What would be the output produced by executing the following C++ code? Identify and correct
errors, if any.
1 #include <iostream>
2 #include<cassert>
3 using namespace std;
4 class Point3D
5 {
6 public:
7 Point3D() {
8 p[0] = p[1] = p[2] = 0;
9 }
10 Point3D::Point3D(int x_, int y_, int z_) {
11 p[0] = x_; p[1] = y_; p[2] = z_;
12 }
13 Point3D operator*(const int & v) {
14 Point3D v1;
15 v1[0] = p[0] + v; v1[1] = p[1] + v; v1[2] = p[2] + v;
16 return v1;
17 }
18
31 Point3D operator-() {
32 Point3D v1;
33 v1[0] = -p[0]; v1[1] = -p[1]; v1[2] = -p[2];
34 return v1;
35 }
36 bool operator==(const Point3D &v) {
37 return p[0] == v[0] && p[1] == v[1] && p[2] == v[2];
38 }
39 int operator[](const int & i) const {
Page 5 of 11 Continue. . .
CS103 - Computer Programming Spring 2014 Sessional II
40 assert(i >= 0 && i <= 2); // check for index with-in range
41 return p[i];
42 }
43 int & operator[](const int & i) {
44 assert(i >= 0 && i <= 2); // check for index with-in range
45 return p[i];
46 }
47 private:
48 int p[3];
49 };
50 ostream &operator<<(ostream &out, const Point3D&v) {
51 out << " X = " << v[0] << " Y = " << v[1] << " Z = " << v[2] << endl
52 << flush;
53 return out;
54 }
55
56 int main() {
57 Point3D p1(10, 20, 30), p2(20, 30, 40);
58 cout << " P1 : " << p1 << " P2 : " << p2 << endl;
59
60 Point3D p3;
61 p3[0] = 5;
62 p3[1] = 5;
63 p3[2] = 5;
64
Page 6 of 11 Continue. . .
CS103 - Computer Programming Spring 2014 Sessional II
Page 7 of 11 Continue. . .
CS103 - Computer Programming Spring 2014 Sessional II
Page 8 of 11 Continue. . .
CS103 - Computer Programming Spring 2014 Sessional II
Demonstrate the classes in a program that has an array of Ship pointers. The array elements should be initialized
with the addresses of dynamically allocated Ship, CruiseShip, and CargoShip objects. The program should then
step through the array, calling each object’s print function.
Page 9 of 11 Continue. . .
CS103 - Computer Programming Spring 2014 Sessional II
Page 10 of 11 Continue. . .
CS103 - Computer Programming Spring 2014 Sessional II
Page 11 of 11 End