PRACTICAL – 1.1
Design a simple class with all arithmetic function. Use
them in MAIN function.
#include <iostream>using namespace std;class Cacl{public:int sum(int a, int b){return (a + b);}int sub(int a, int b){return (a - b);}int mul(int a, int b){return (a * b);}int div(int a, int b){return (a / b);}};int main(){Cacl r;int x, y;cout << "Enter the value : ";cin >> x >> y;cout << "Sum =" << r.sum(x, y) << endl;cout << "Sum =" << r.sub(x, y) << endl;cout << "Sum =" << r.mul(x, y) << endl;cout << "Sum =" << r.div(x, y) << endl;return 0;}
PRACTICAL – 1.2
Create a class student with student name and age as data
members. Define functions to read and display the data members.
#include <iostream>#include <string>using namespace std;class School{string name_s;int age;public:void setdata(){cout << "Enter name and age : ";cin >> name_s >> age;}void getdata(){cout << "Name : " << name_s << endl<< "age : " << age << endl;}};int main(){School s1, s2;s1.setdata();s2.setdata();s1.getdata();s2.getdata();return 0;}
PRACTICAL – 1.3
Create a String class that includes all the string-related
functions. Like Length, copy, compare, concatenation, sub string search
(Without using inbuilt string functions).
#include <iostream>#include <string>using namespace std;class String{public:int str_len(string s){int len = 0;for (int i = 0; s[i] != '\0'; i++){len++;}return len;}int str_cmp(string s1, string s2){int a = s1.length(), b = s2.length();int max = a > b ? a : b;int comp;for (int i = 0; i < max; i++){if (s1[i] == s2[i]){comp = 0;continue;}else if (s1[i] > s2[i]){comp = -1;break;}else{comp = 1;break;}}return comp;}string str_con(string s1, string s2){return (s1 + s2);}void str_cpy(){string s1, s2;cout << "Enter String";cin >> s1;int i;for (i = 0; s1[i]; i++)s2[i] = s1[i];s2[i] = '\0';cout << " copied string is: " << s2;}void str_sub(string s1, string s2){for (int i = 0; s1[i] != '\0'; i++){int j, k;for (j = 0, k = 0; s1[j] != '\0' && s2[k] != '\0'; j++){if (s1[j] == s2[k]){k++;}else{k = 0;}}if (k == i){cout << "Substring found at position " << j - k;break;}else{cout << "Substring not found";}}}};int main(){String s, r;string s1 = "Rahi", s2 = "Patel";cout << "Length : " << s.str_len(s1) << endl;cout << "Compair : " << s.str_cmp(s1, s2) << endl;cout << "Concate : " << s.str_con(s1, s2) << endl;s.str_sub(s2, s1);return 0;}
0 Comments