Question:7
Class time (h, m and s)
C++ program to declare a class named time with attributes as h, m and s. Take input for two times calculate and display their sum.
Sol:
Solution In CodeBlocks/DevC++
#include<iostream> #include<conio.h> using namespace std; class time { private: int h,m,s; public: void read(); void show(); friend time add(time t1,time t2); }; void time::read() { cout<<"enter h,m and s "; cin>>h>>m>>s; } void time::show() { cout<<h<<" : "<<m<<" : "<<s<<endl; } time add(time t1,time t2) { time t; t.h=t1.h+t2.h; t.m=t1.m+t2.m; t.s=t1.s+t2.s; if(t.s>=60) { t.m=t.m+t.s/60; t.s=t.s%60; } if(t.m>=60) { t.h=t.h+t.m/60; t.m=t.m%60; } return(t); } int main() { /* clrscr(); */ time e1,e2,e3; e1.read();e2.read(); e1.show();e2.show(); e3=add(e1,e2); e3.show(); getch(); return(0); }
/* Output */ enter h,m and s 5 35 45 enter h,m and s 8 45 50 5 : 35 : 45 8 : 45 : 50 14 : 21 : 35
Solution In Turboc3
/* this program is developed in turboc3 */ #include<iostream.h> #include<conio.h> class time { private: int h,m,s; public: void read(); void show(); friend time add(time t1,time t2); }; void time::read() { cout<<"enter h,m and s "; cin>>h>>m>>s; } void time::show() { cout<<h<<" : "<<m<<" : "<<s<<endl; } time add(time t1,time t2) { time t; t.h=t1.h+t2.h; t.m=t1.m+t2.m; t.s=t1.s+t2.s; if(t.s>=60) { t.m=t.m+t.s/60; t.s=t.s%60; } if(t.m>=60) { t.h=t.h+t.m/60; t.m=t.m%60; } return(t); } void main() { clrscr(); time e1,e2,e3; e1.read();e2.read(); e1.show();e2.show(); e3=add(e1,e2); e3.show(); getch(); }
/* Output */ enter h,m and s 5 35 45 enter h,m and s 8 45 50 5 : 35 : 45 8 : 45 : 50 14 : 21 : 35
Question:8
Class string(str)
C++ program to declare a class named string with attribute as “str”. Take input for two strings and concatenate(join) them.
Sol:
Solution In CodeBlocks/DevC++
#include<iostream> #include<conio.h> #include<string.h> using namespace std; class string1 { char str[20]; public: void read(); void show(); friend string1 add(string1 s1,string1 s2); }; void string1::read() { cout<<"Enter any string "; cin>>str; } void string1::show() { cout<<"string is "<<str<<endl; } string1 add(string1 s1,string1 s2) { string1 t; strcpy(t.str,s1.str); //strcat(t.str," "); strcat(t.str,s2.str); return(t); } int main() { /* clrscr(); */ string1 e1,e2,e3; e1.read();e2.read(); e1.show();e2.show(); e3=add(e1,e2); e3.show(); getch(); return(0); }
/* Output */ Enter any string Hello Enter any string World string is Hello string is World string is HelloWorld
Solution In Turboc3
/* this program is developed in turboc3 */ #include<iostream.h> #include<conio.h> #include<string.h> class string { char str[20]; public: void read(); void show(); friend string add(string s1,string s2); }; void string::read() { cout<<"Enter any string "; cin>>str; } void string::show() { cout<<"string is "<<str<<endl; } string add(string s1,string s2) { string t; strcpy(t.str,s1.str); //strcat(t.str," "); strcat(t.str,s2.str); return(t); } void main() { clrscr(); string e1,e2,e3; e1.read();e2.read(); e1.show();e2.show(); e3=add(e1,e2); e3.show(); getch(); }
/* Output */ Enter any string Hello Enter any string World string is Hello string is World string is HelloWorld
Question:9
Class complex(real,img)
C++ program to declare a class named complex with attributes as real and img. Take input for two complex numbers calculate and display their sum in the given format.
A + i B
A – i B
Real +/- i img
Sol:
Solution In CodeBlocks/DevC++
#include<iostream> #include<conio.h> #include<string.h> using namespace std; class complex { private: int real,img; public: void read(); void show(); friend complex add(complex c1,complex c2); }; void complex::read() { cout<<"Enter values of real and img "; cin>>real>>img; } void complex::show() { if(img>=0) cout<<real<<" +i "<<img<<endl; else cout<<real<<" -i "<<img*-1<<endl; } complex add(complex c1,complex c2) { complex t; t.real=c1.real+c2.real; t.img=c1.img+c2.img; return(t); } int main() { /* clrscr(); */ complex e1,e2,e3; e1.read();e2.read(); e1.show();e2.show(); e3=add(e1,e2); e3.show(); getch(); return(0); }
/* Output */ Ex:1 Enter values of real and img 5 6 Enter values of real and img 8 9 5 +i 6 8 +i 9 13 +i 15 Ex:2 Enter values of real and img 5 -9 Enter values of real and img 3 -7 5 -i 9 3 -i 7 8 -i 16
Solution In Turboc3
/* this program is developed in turboc3 */ #include<iostream.h> #include<conio.h> class complex { private: int real,img; public: void read(); void show(); friend complex add(complex c1,complex c2); }; void complex::read() { cout<<"Enter values of real and img "; cin>>real>>img; } //**** void complex::show() { if(img>=0) cout<<real<<" +i "<<img<<endl; else cout<<real<<" -i "<<img*-1<<endl; } complex add(complex c1,complex c2) { complex t; t.real=c1.real+c2.real; t.img=c1.img+c2.img; return(t); } void main() { clrscr(); complex e1,e2,e3; e1.read();e2.read(); e1.show();e2.show(); e3=add(e1,e2); e3.show(); getch(); }
/* Output */ Ex:1 Enter values of real and img 5 6 Enter values of real and img 8 9 5 +i 6 8 +i 9 13 +i 15 Ex:2 Enter values of real and img 5 -9 Enter values of real and img 3 -7 5 -i 9 3 -i 7 8 -i 16