You are on page 1of 1

Lab Assignment 2

1. Write a C++ program to implement the concept of smart pointers.

2. Write a C++ program to demonstrate working of auto, type inference, and use of
decltype.

3. An election is conducted by five candidates. The candidates are numbered 1 to 5 and the
voting is done by marking the candidate number on the ballot paper. Write a program to
read the ballots and count the votes cast for each candidate using an array variable count.
In case, a number read is outside the range 1 to 5, the ballot should be considered as a
“spoilt ballot”, and the program should also count the number of spoilt ballots.

4. Write a C++ program to implement the matrix ADT using a class. Use operator
overloading for implementation.

5. Write a C++ program to maintain the records of person with details (Name and Age) and
find the eldest among them. The program must use this pointer to return.

6. Write a Program to design a student a student class representing student roll no., and a
test class (derived class of student) representing the scores of the student in various
subjects and sports class representing the scores in sports. The sports and test class should
be inherited by a result class having the functionality and to add the scores and display
the final result for a student.

7. Write a C++ program to allocate memory dynamically for an object of a given class using
class’s constructor.

8. Four classes, C1, C2, C3, and C4 have been given in following figures along with main().
Get the output of the C++ program without running the code.

class C1 class C3
{ {
int x; C1 *objC1type[2];
. C2 objC2type[2];
public: public:
C1(){ x = 50; cout<<"\n Print 1"; } C3(){ cout<<"\n Print 7";}
C1(int temp){ x = temp; cout<<"\n Print 2"; } C3(int temp){ cout<<"\n Print 8"; }
void f1(){ cout<<"\nEnter X"; cin>>x; } void f7(int temp){ objC1type[0] = new C1; objC1type[1] = new C1(temp); }
void f2(int temp){ x = temp; } void f8(){ C1 *temp1 = new C1(20); C2 temp2; temp1 -> f3(); temp2.f6(); }
void f3(){ cout<<"\nX in C1 is = "<<x; } void f9(C1 *temp1)
~C1(){ cout<<"\n Print 3"; } {
}; C1 *temp2, *temp3, temp4; temp2 = temp1; temp2->f2(30); temp3 = new C1;
temp3->f2(40); temp4.f2(70);
class C2 temp1 -> f3(); temp2 -> f3(); temp3 -> f3(); temp4.f3();
{ }
int y; ~C3(){ cout<<"\n Print 9"; }
};
public: class C4
C2(){ y = 60; cout<<"\n Print 4"; } {
C2(int temp){ y = temp; cout<<"\n Print 5"; } C3 *obj1, obj2;
void f4(){ cout<<"\nEnter Y"; cin>>y; } public:
void f5(int temp){ y = temp; } C4(){ obj1 = new C3; cout<<"\n Print 10"; }
void f6(){ cout<<"\nY in C2 is = "<<y; } void f10(){ C3 *temp1, temp2; temp1 = new C3(10); }
~C2(){ cout<<"\n Print 6"; } ~C4(){ cout<<"\n Print 11"; }
}; };

int main()
{ C1 *objC1 = new C1; objC1 -> f3(); C3 objC3; C4 objC4; objC3.f7(5); objC3.f8(); objC3.f9(objC1); objC4.f10(); objC1 -> f3(); return 0; }

You might also like