CS103 Computer Programming Mid-II: National University of Computer and Emerging Sciences
CS103 Computer Programming Mid-II: National University of Computer and Emerging Sciences
I II III Total
Total Marks 35 20 20 75
Marks Obtained
(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 int len;
5 int *ptr;
6 public:
Page 2 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II
7 A() {
8 len = 0;
9 ptr = NULL;
10 }
11 A(int l, int *arr) {
12 initialize(l);
13 for (int i = 0; i < l; i++)
14 ptr[i] = arr[i];
15 }
16 void initialize(int l) {
17 ptr = new int[l];
18 for (int i = 0; i < l; i++)
19 ptr[i] = 0;
20 }
21 void operator=(const A &a) {
22 len = a.len;
23 for (int i = 0; i < len; i++)
24 ptr[i] = a.ptr[i];
25 }
26 A operator+(const A a) {
27 A temp;
28 temp.initialize(len);
29 for (int i = 0; i < len; i++)
30 temp.ptr[i] = ptr[i] + a.ptr[i];
31 return temp;
32 }
33 int& operator()(int i) const {
34 if (i < len)
35 return ptr[i];
36 }
37 int GetLen() const {
38 return len;
39 }
40
41 ˜A() {
42 if (ptr != NULL)
43 delete[] ptr;
44 }
45 };
46
53 int main() {
54 int arr1[] = { 1, 2, 3, 4, 5 };
55 int arr2[] = { 5, 4, 3, 2, 1 };
56 A v1(5, arr1), v2(5, arr2), v3;
57 cout << v1;
58 cout << (v1 + v2);
59 v3 = v2;
Page 3 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II
(3) (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 Mystery {
4 int * p;
5 public:
6 Mystery(int value = 0) {
7 p = new int;
8 *p = value;
9 }
10 int getP() {
11 return *p;
12 }
13 bool setP(int x) {
14 *p = x;
15 }
16 Mystery & operator=(const Mystery& t) {
17 p = new int;
18 *p = *t.p;
19 }
20 Mystery add(const Mystery &toAdd) {
21 Mystery Res = toAdd;
22 *Res.p += *p + *toAdd.p;
23 return Res;
24 }
25 ˜Mystery() {
26 delete p;
27 }
28 };
29 int main() {
30 Mystery obj_one(3);
31 Mystery obj_two(6);
32 obj_one.add(obj_two);
33 cout << obj_two.getP();
34 return 0;
35 }
Page 4 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II
(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 ShoppingCart {
4 private:
5 static int itemsCount;
6 string itemName;
7 public:
8 ShoppingCart() :
9 itemName("") {
10 }
11 ShoppingCart(string n) :
12 itemName(n) {
13 }
14 void setItemsCount(int count) {
15 this->itemsCount = count;
16 }
17 static int getItemsCount() {
18 return itemsCount;
19 }
20 };
21 int main() {
22 ShoppingCart Sc_one;
23 ShoppingCart Sc_two("1");
24 Sc_one.setItemsCount(5);
25 Sc_two.setItemsCount(10);
26 cout << ShoppingCart::getItemsCount();
27 return 0;
28 }
Page 5 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II
(5) (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
4 class Point {
5 int x, y;
6 public:
7 Point(int a = 0, int b = 0) {
8 x = a;
9 y = b;
10 print();
11 }
12 void print() {
13 cout << " (" << x << "," << y << ") " << endl;
14 }
15 ˜Point() {
16 cout << "Point is going" << endl;
17 }
18 };
19 class Circle {
20 Point center;
21 float radius;
22 public:
23 Circle() :
24 center(0, 0) {
25 radius = 0;
26 cout << "The basic circle" << endl;
27 }
28 Circle(Point p) :
29 center(p) {
30 }
31 Circle(const Circle & c) :
32 center(c.center), radius(c.radius) {
33 cout << "The copied circle";
34 center.print();
35 }
36 ˜Circle() {
37 cout << "Circle is going" << endl;
38 }
39 };
40 int main() {
41 Point p1;
42 Circle c1(p1);
Page 6 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II
(6) (10 Marks) Identify and correct the errors in the following code so that the main() function could run (You
can add new functions if needed). You cannot change the main() function!
1 #include<iostream>
2 using namespace std;
3 class Student{
4 string name;
5 string rollno;
6 public:
7 Student(string n, string r){ name=n;rollno=r;}
8 };
9 class Class {
10 const int nstudents;
11 Student s[10];
12 public:
13 Class() {
14 nstudents=10;
15 }
16 Class(const Class c)
17 {
18 *this=c;
19 }
20 void operator<<(const ostream & out)
21 {
22 for(int i=0; i < nstudents;++i)
23 out<< s[i];
24 }
25 Student operator[](int & i)
26 {
27 if(i >=0 && i < nstudents)
28 return s[i];
29 }
30 };
31 Class operator=(const Class & c1, const Class & c2)
32 {
33 for(int i=0; i < nstudents;++i)
34 c1[i]=c2[i];
35 }
36 int main() {
37 Class c, c2;
38 c2=c;
39 c2<<cout;
40 c<<cout;
Page 7 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II
41 return 0;
42 }
Page 8 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II
Page 9 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II
Page 10 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II
Page 11 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II
(2) (15 Marks) Now write all complete code with the required data members, member functions and operators
Page 12 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II
Page 13 of 13 End