You are on page 1of 7

Q1.

Declare a class Time with data members of hours, min & sec to store the hour, minutes and seconds for any duration of time .Write a program to read two times & add them. #include<iostream.h> #include<conio.h> class Time{ int hours, minutes, seconds; public: Time(): hours=0, minutes=0, seconds=0 {} Time(int hr, int mt, int sc): hours=hr, minutes=mt, seconds=sc {} void display() { cout<<hours<<":"<<minutes<<":"<<seconds; } void add(time x, time y) { float tmp; tmp=x.hours*3600+x.minutes*60+x.seconds+y.hours*3600+y.minutes*60+y.seconds; seconds=tmp%60; minutes=((tmp-seconds)%3600)/60; hours=tmp/3600; if(seconds>59) {seconds=seconds-59; minutes++; } if(minutes>59) {minutes=minutes-59; hours++; } } ~Time() { } }; void main() { Time t1(12, 34, 4), t2(3, 34, 45); Time t3; t3.add(t1, t2); t3.display();

} Q2. Write an object oriented program to find the sum of two matrices so that we are able to write c=a+b where a,b,c are matrices. Answer :#include<iostream.h> #include<conio.h> #include<stdlib.h> class matrix { int a[4][4],b[4][4],c[4][4],i,j,row1,col1,row2,col2; public: void getvalues(); void displaysum(); }; void matrix::getvalues() { cout<<"Enter the size of the row and column of matrices first"; cin>>row1>>col1; cout<<"Enter the size of the row and column of matrices Second"; cin>>row2>>col2; if(row1!=row2) { if(col1!=col2){ cout<<"The size of the rows and columns should be equal"; getch(); exit(0); } } cout<<"Enter values for the matrix A\n"; for(i=0;i<row1;i++) for(j=0;j<col1;j++) cin>>a[i][j]; cout<<"Enter the values for matrix b\n"; for(i=0;i<x;i++) for(j=0;j<y;j++) cin>>b[i][j]; } void matrix::displaysum() { cout<<"The sum of matrix A and B is\n"; for(i=0;i<row1;i++) { for(j=0;j<col1;j++)

c[i][j]=a[i][j]+b[i][j]<<"\t"; cout<<c[i][j]; cout<<endl; } } void main() { clrscr(); matrix m; m.getvalues(); m.displaysum(); getch(); } Q3. Write an object oriented program to find the sum of two matrices so that we are able to write c=a*b where c,a,b are matrices. #include<iostream.h> #include<conio.h> #include<stdlib.h> class matrix { int a[4][4],b[4][4],c[4][4],i,j,row1,col1,row2,col2; public: void getvalues() { cout<<"Enter the size of the row and column of matrices first"; cin>>row1>>col1; cout<<"Enter the size of the row and column of matrices Second"; cin>>row2>>col2; if(col1!=row2) { cout<<"The size of the row of 2 and column of 1 should be equal"; getch(); exit(0); } } cout<<"Enter values for the matrix A\n"; for(i=0;i<row1;i++) for(j=0;j<col1;j++) cin>>a[i][j]; cout<<"Enter the values for matrix b\n";

for(i=0;i<x;i++) for(j=0;j<y;j++) cin>>b[i][j]; } void displaysum() { cout<<"The sum of matrix A and B is\n"; for(i=0;i<row1;i++) { for(j=0;j<col1;j++) c[i][j]=0; for(k=0;k<0;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]<<"\t"; cout<<c[i][j]; cout<<endl; } } ~matrix() { } }; void main() { clrscr(); matrix m; m.getvalues(); m.displaymul(); getch(); } Q4.Write a program that declares the classes and defines member functions to read two numbers and find their sum, difference, product and ratio. Write function main() to read users option and the two numbers and then to complete arithmetic operation as desired by the user.(hint : use concept of run time polymorphism). Answer :include <iostream.h> using namespace std; class Calc { int x, y; public: void set_values (int,int); int sum () {

return (x+y); } int diff () { return (x-y); } int mul () { return (x+y); } int ratio () { return (x/y); } }; void Calc::set_values (int a, int b) { x = a; y = b; } int main () { int num1,num2,chioce; Calc rect; cout<<Enter two numbers and operation you want to perform for addition(1),subtraction(2),Multiplication(3),Ratio(4) ; cin>>num1>>num2; cin>>choice rect.set_values (num1,num2); switch(choice) { case 1: { cout << "sum: " << rect.sum(); break; } Case 2: { cout << "Difference: " << rect.diff(); break;

} Case 3 : { cout << "Mul: " << rect.mul(); break; } Case 4: { cout << "Ratio: " << rect.ratio(); break; } Default : cout<<Your have wrong choice ; } Q5. A company forecasts the sales of a product by two methods-method of simple average and weighted average. If s1,s2,s3 are the sales of product one,two,three years back the method of simple average assumes the sales of the coming year to be (s1+s2+s3)/3.The method of weighted average gives weights of 0.5 ,0.3, 0.2 to s1,s2,s3 and assume the coming year sales to be 0.5*s1+0.3*s2+0.2*s3. Write a program to read the sales for last 3 years and forecasts the sale of coming year by both the methods. Use a constructor to initialize the weights for the methods of weighted average. (apply concept of inheritance). Answer :class simple_avg { protected: int s1,s2,s3; public: void initialize(int in_s1, int in_s2,int in_s3); }; void simple_avg::initialize(int in_s1, int in_s2,int in_s3) { S1 = in_s1; S2=in_s2; S3=in_s3; } class w_avg : public simple_avg { public: void assume(void){ s=(s1+s2+s3)/3;

w=0.5*s1+0.3*s2+0.2*s3; cout<<s<<w; } void main() { w_avg f; f.initialize(4, 3,2); f.assume(); } Q6. Create a class vector that is having: a) One default constructor to initialize all elements to 0 b) And another constructor that accepts pointer to array and initialize all its elements by array elements. c) Overload the operator * for scalar multiplication (For example it should be able to perform both operations for v i.e. 2*v or v*2). Answer :-

You might also like