0% found this document useful (0 votes)
10 views17 pages

LAB Practical Solution

The document outlines various C++ programming exercises focusing on Object-Oriented Programming (OOP) concepts. It includes algorithms for summing digits, generating sequences, finding prime numbers, and implementing classes with constructors, operator overloading, and inheritance. Additionally, it covers dynamic memory management, exception handling, and template functions for sorting and finding minimum/maximum values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
10 views17 pages

LAB Practical Solution

The document outlines various C++ programming exercises focusing on Object-Oriented Programming (OOP) concepts. It includes algorithms for summing digits, generating sequences, finding prime numbers, and implementing classes with constructors, operator overloading, and inheritance. Additionally, it covers dynamic memory management, exception handling, and template functions for sorting and finding minimum/maximum values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Lab: OOPS using C++ 1. Sum of digits of positive integer Algorithm: Repeatedly take n % 10 and add to sum, then don /= 19 until n == 0. dinclude using namespace std; int nain() £ int n, sum = 0; cout << "Enter a positive integer: " cin >> while (n > 0) { sum += 7 % 10; fn /= 10; + cout << "Sum of digits = " << sum << end! zeturn 0; 2. Generate first n terms of a sequence Assume simple arithmetic progression: 1, 2, 3 (adapt/change as per your teacher's given formula). Winclude using namespace std; Ant main) £ int a; cout << "Enter n: "; cin >> ny for (int i= 1; i <=n; +4) cout ce ice"; return 0; 3. Primes between 1 andn Check each number from 2 to n for primality by trial division up to YL Winclude #include using namespace st bool isPrime(int x) { if (x < 2) return false; for (int i 2) d= sqrt; +74) Sf (x i == 6) return false; return true; ¥ int nain() £ int n; cout << “Enter n: *; cout << "Primes between 1 and "cen ce": "; for (int i= 2; 4 44i) if (isPrime(i)) cout << i <<" zeturn ¥ 4, Largest and smallest in a list Read a numbers, maintain min and max while reading LiL Winclude Winclude using namespace std; int nain() £ int n; cout << "How many numbers? "; cin >> int x; int mn = numezic_limits: :max(); int mc = numeric_limitscint>: :min(); for (int i= 0; i> Gf (x < mn) mm = x; Gf (x > mx) me = x; t cout << "Smallest = " << mn << "\nlargest = " << mx << endl; return 0; 5. Sort list in ascending order Use simple bubble sort. Winclude using namespace std; int nain() £ int n; cout << “Enter nunber of elenents: “; int a{™100]; for (int i= 0; i > al for (int i= 0; i ali +1) swap(aj], al} + 10); cout << "Sorted list: "; for (int i= 0; i > int *arr = new int[n]; cout << "Enter " << n << " elements:\n' for (int i= 0; i > arr[il; cout << "You entezed for (int i= 0; i < nj +44) cout using namespace std; class Rectangle { int length, braadth; public void setData(int 1, int b) { Length = 1; breadth = b; } int area() £ zeturn length * breadth; + h int nain() £ Rectangle 1; r-setData(10, 5) cout << “Area = * << razea() << endl; seturn 0; ¥ 8. Default, parameterized, copy constructors Showall three in a Point class include using namespace std: class Point { int x, y7 public point() £ x= 0; y = 9; 3 Wf cetauit Point(int a, int b) {x = a; y = b; F // pazaneterized Point(const Point &p) {x= p.x: y= p.y: } // copy void display() { cout << "(" @include using namespace std; class STUDENT { string StucentVane; int Marks (*5]; int Total; float Avg; int Maxtiazks; public: void assign() cout << "Enter student name: "; getline(cin >> us, StudentNane cout << "Enter § subject mazks: Maxtiazks = 100; for (int i= 6; i < 5; +44) cin >> maxks(i]; } void conpute() Total = for (int i= 6; i< 5; ++i) Total += Marks[i]; Avg = Total / 5.08; r void display() { cout << “Name: * << Studentuame << “\nTotal: * << Total << "\nkverage: * << Avg << endl; 7 int main() { STUDENT s. [Link](); [Link]() [Link]; return 0; 3 10. Operator and function overloading Overload unary - and + and also overload add function. Winclude using namespace std; class Complex { int real, imag; public: Complex (int int i= 0) : real(z), imag(i) 1} 1/ operator overloading Complex operator+(const Complex &c) const { return Complex(real + [Link], imag + c. imag); r Complex operator-() const £ If wary minus xeturn Complex(-real, imag); 3 I/ function overloading void display() const £ cout << real << "+" << imag << * void display (const string Smeg) const [ cout << mag ce": " ce zeal ce" 4" ce imag ce "i\n"; int nain() £ Complex c1(2, 3), c2(4, 5); Complex c3 Complex ca cL + 25 oct [Link]("e1"); splay("c2"); 3. display("c3") [Link]("ca"); xeturn 0; 2. 11. Friend function and friend class Use a friend function to access private members; a friend class to access them too. dinclude using namespace std; class Box; class BoxHelper { public: void showvclume(const Box &b) ; i class Box { int length, breadth, height; public: // friend class Box(int 1, int b, int h) : length(1), breadth(b), height(h) 1} friend int volume(const Box &b); Erlend class BoxHelper; II friend function Wf friend class int volume(const Box &b) £ zeturn [Link] * [Link] * [Link]; void BoxHelper::shonVelume(const Box &b) { cout << "Volume from friend class << belength * [Link] * bsheight << endl; ¥ int nain() £ Box b(2, 3, 4); cout << "Volume from friend function = " << volune(b) << endl; BoxHelpex th; bh. showvolume (b) return ¥ 12. Pointer to object members Use pointer to object to access members via ->41L Winclude using namespace std class STUDENT { int roll float marks; public: void setData(int r, float m) { roll = r; marks =m; F void display() { cout << "Roll: " << roll << " Marks: " << marks << endl; } h int nain() £ STUDENT s; STUDENT xptr = &s; ptr-ssetData(1, 95.54); ptr-sdisplay(); zeturn 0; 13. Fibonacci using constructor Constructor initializes first two terms and count. Winclude using namespace std; class Fibonacci 4 int ny a, b; public: Fibonacci (int terms) : n(terms), a(@), b(1) 4% void generate() { if (n < 0) return; cout Fibonacci £(n); [Link](); zeturn 0; 14. Matrix ADT class Support read, print, add, subtract, multiply. Keep itsimple for small matrices. finclude using namespace std: class Matrix { int x, ¢; int a[*10] [910] ; public: Natrix(int xows = 0, int cols = 0) : x(zows), (cols) {} void read() £ cout << “Enter rous and cols. cin >> 5 >> ¢} cout << "Enter elements:\n"; for (int i= 0; i < 1; +i) for (int j= 9; j < cy +43) cin >> ali] Li]: void print() const { for (int i= 6; i< xz; +i) f for (int j = 9; j using namespace std; class Base { public: virtual void show() { cout << "Base show\n"; Bh clase Derived : public Base { public void show() override { cout << "Derived show\n"; } h int nain() £ Base *ptr; Derived d; ptr = ptr->show(); // calls Derived: :show because of virtual zeturn ¥ 18. Template-based sort Use function template with simple bubble sort) Winclude using namespace std; ‘template void sortList(T af], int 9) { for (int i= 0; i alj +1) swap(atj], alj + 11); ant main) £ int a; cout << "Enter nunber of elenents: cin >> ny int a[*100] for (int i= 0; i > al sortList(a, n); cout << "Sorted: "; for (int i= 0; i < nj +44) cout using namespace std; ‘template T getmin(T al], ant n) £ T mn = ae]; for (int i= 4; i < ny 443) alil: if (ali) < mn) m zeturn an; ‘template T gethax(T al], int n) £ T mx = a0]; for (int i= 4; 2< nj 443) if (ai) > mo) mx = ali: return mx; template void sortList(T a[], int n) 7 for (int 4-0; 4 ali +1) swap(ali], ali +11); ant nain() £ int a; cout << "Enter n int a[*100] for (int i 0; > ali cout << "Min =" << getMin(a, n) << "\nMax = " << getMax(a, n) << end sortList(a, n); cout << "Sorted: *; for (int i= 0; i > by ty t if (b == 0) ‘throw "Division by zezo!"; double result = static_cast(a) / b; cour << "Result = * << zesult << endl; } catch (const chaz snag) £ cout << "Exception caught: * << msg << endl: } zeturn 0;

You might also like