C++

C++ Programming

Slip6

Q.1) Write a C++ program to create a class which contains two dimensional integer array of
size mXn. Write a member function to display sum of all elements of entered matrix.
(Use Dynamic Constructor for allocating memory and Destructor to free memory of an
object) [Marks 15]

Solution

#include<iostream.h>
#include<conio.h>
class array2
{
int **arr;
int m,n;
public:
array2(){}
array2(int,int);
void display()
{
int sum=0;
cout<<"enter "<<m*n<<" elements";
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>arr[m][n];
sum+=arr[m][n];
}
}
cout<<"addition="<<sum;
}
~array2()
{
   for(int i=0;i<m;i++)
delete arr[i];
}
};
array2::array2(int a,int b)
{
m=a;
n=b;
       arr=new int*[m];
for(int i=0;i<m;i++)
arr[i]=new int[n];

}
int main()
{       clrscr();
array2 obj;
int a,b;
cout<<"enter no of rows";
cin>>a;
cout<<"enter no of cols";
cin>>b;
obj=array2(a,b);
obj.display();
getch();
return 0;
}

Output

enter no of rows3
enter no of cols3                                                             
enter 9 elements
9
8                                                                             
7                                                                             
3                                                                             
2                                                                             
1                                                                             
4                                                                             
5                                                                             
6                                                                             
addition=45Null pointer assignment


Q.2) Create a class Time which contains data members as: Hours, Minutes and Seconds. Write
C++ program to perform following necessary member functions:
i. To read time
ii. To display time in format like: hh:mm:ss
iii. To add two different times (Use Objects as argument) [Marks 25]                                            
                                    

Solution

#include<iostream.h>
#include<conio.h>
class time
{
int hh,mm,ss;
public:
void read()
{
cout<<"Enter time in hh:mm:ss  format";
cin>>hh>>mm>>ss;
}
void display()
{
cout<<"\n"<<hh<<":"<<mm<<":"<<ss;
}
time add(time t1,time t2)
{
time t3;
int temp=0;
temp=(t1.ss+t2.ss)/60;
t3.ss=(t1.ss+t2.ss)%60;
temp=(t1.mm+t2.mm+temp)/60;
t3.mm=(t1.mm+t2.mm)%60;
t3.hh=t1.hh+t2.hh+temp;
return t3;
}
};
int main()
{
 int choice;
 time obj1,obj2,obj3;
 do
 {
cout<<"\n 1: read time \n 2: Add two different time \n 3:Display  time \n 4:exit\n";
cout<<"Enter your choice";
cin>>choice;
switch(choice)
{
case 1: obj1.read();
obj2.read();
break;
case 2: obj3=obj3.add(obj1,obj2);
break;
case 3: obj1.display();
obj2.display();
obj3.display();
break;
}
 }while(choice!=4);
 getch();
 return 0;
}
BCA Pratical Solution

My name is Vivek And I from Mumbai and Complete my Graduation Bca.my Age is 23 Years.

Post a Comment

Previous Post Next Post