// Write a program to define a class Book having data members as // author_Name and book_Price. Accept and display data for two book // objects and display details of book having higher price. #include <iostream> #include <stdlib.h> #include <string> #include <conio.h> using namespace std; class Book { string authorName; float bookPrice; public: void getBookDetails() { cout << "Enter Author Name: "; cin >> authorName; cout << "Enter Book Price: "; cin >> bookPrice; } void showBookDetails() { cout << "Author Name: " << authorName << endl; cout << "Book Price: " << bookPrice << endl; } float getBookPrice() { return bookPrice; } }; int main() { Book b1, b2; b1.getBookDetails(); b2.getBookDetails(); if (b1.getBookPrice() > b2.getBookPrice()) { b1.showBookDetails(); } else { b2.showBookDetails(); } return 0; } // Write a C++ program to declare class mobile having data members as // price and model number. Accept data for 5 objects and display details // of mobile having price greater than 12000. #include <iostream> #include <stdlib.h> #include <string> #include <conio.h> using namespace std; class Mobile { int modelNo; float price; public: void getMobileDetails() { cout << "Enter Model No: "; cin >> modelNo; cout << "Enter Price: "; cin >> price; } void showMobileDetails() { cout << "Model No: " << modelNo << endl; cout << "Price: " << price << endl; } float getMobilePrice() { return price; } }; int main() { Mobile m[5]; for (int i = 0; i < 5; i++) { m[i].getMobileDetails(); } for (int i = 0; i < 5; i++) { if (m[i].getMobilePrice() > 12000) { m[i].showMobileDetails(); } } return 0; } // Develop a C++ code to print sum of array elements. #include <iostream> #include <stdlib.h> #include <string> #include <conio.h> using namespace std; int main() { int a[5], i, sum = 0; for (i = 0; i < 5; i++) { cout << "Enter a number: "; cin >> a[i]; } for(i = 0; i < 5; i++) { sum = sum + a[i]; } cout << "Sum of all numbers is: " << sum << endl; return 0; } // Write a program in C++ to overload unary ‘--’ operator to decrease value // of data member of class. #include <iostream> #include <stdlib.h> #include <string> #include <conio.h> using namespace std; class Decrement { int a; public: Decrement(int x) { a = x; } Decrement operator -- () { return Decrement(--a); } void display() { cout << "a: " << a << endl; } }; int main() { Decrement d1(10), d2(20); d1.display(); d2.display(); d1 = --d1; d2 = --d2; d1.display(); d2.display(); return 0; } // Write a program in C++ to overload unary ‘++’ operator to increase value // of data member of class. #include <iostream> #include <stdlib.h> #include <string> #include <conio.h> using namespace std; class Increment { int a; public: Increment(int x) { a = x; } Increment operator ++ () { return Increment(++a); } void display() { cout << "a: " << a << endl; } }; int main() { Increment i1(10), i2(20); i1.display(); i2.display(); i1 = ++i1; i2 = ++i2; i1.display(); i2.display(); return 0; } // Write a program in C++ to overload unary ‘-’ operator to negate values of // data members of class. #include <iostream> #include <stdlib.h> #include <string> #include <conio.h> using namespace std; class Negate { int a, b; public: Negate(int x, int y) { a = x; b = y; } Negate operator-() { return Negate(-a, -b); } void display() { cout << "a: " << a << endl; cout << "b: " << b << endl; } }; int main() { Negate n1(10, 20), n2(-10, -20); n1.display(); n2.display(); n1 = -n1; n2 = -n2; n1.display(); n2.display(); return 0; } // Write a program in C++ to overload unary ‘+’ operator to concatenate // two strings. #include <iostream> #include <stdlib.h> #include <string> #include <conio.h> using namespace std; class String { string str; public: String(string s) { str = s; } String operator+(String s) { return String(str + s.str); } void display() { cout << str << endl; } }; int main() { String s1("Hello"), s2("World"); String s3 = s1 + s2; s3.display(); return 0; } // Write a C++ program to declare a class product with members as // product_no and name. Accept and display data for one object of product. // Use pointer to object to call functions of class. #include <iostream> #include <stdlib.h> #include <string> #include <conio.h> using namespace std; class Product { int productNo; string productName; public: void getDetails() { cout << "Enter product number: "; cin >> productNo; cout << "Enter product name: "; cin >> productName; } void displayDetails() { cout << "Product number: " << productNo << endl; cout << "Product name: " << productName << endl; } }; int main() { Product *p; p = new Product; p->getDetails(); p->displayDetails(); return 0; } // Write a C++ program to declare a class employee with members as // employee_no and name. Accept the information for 5 objects and display // it using pointer to array of objects. #include <iostream> #include <stdlib.h> #include <string> #include <conio.h> using namespace std; class Employee { int employeeNo; string employeeName; public: void getDetails() { cout << "Enter employee number: "; cin >> employeeNo; cout << "Enter employee name: "; cin >> employeeName; } void displayDetails() { cout << "Employee number: " << employeeNo << endl; cout << "Employee name: " << employeeName << endl; } }; int main() { Employee *e[5]; for(int i=0; i<5; i++) { e[i] = new Employee; e[i]->getDetails(); } for(int i=0; i<5; i++) { e[i]->displayDetails(); } return 0; } // Write a program to overload ‘+‘ operator to add two time objects having // data members as hours and minutes. Display the result using third // object. #include <iostream> #include <stdlib.h> #include <string> #include <conio.h> using namespace std; class Time { int hours, minutes; public: Time() { hours = 0; minutes = 0; } Time(int h, int m) { hours = h; minutes = m; } void display() { cout << "Hours: " << hours << endl; cout << "Minutes: " << minutes << endl; } Time operator+(Time t) { Time temp; temp.hours = hours + t.hours; temp.minutes = minutes + t.minutes; return temp; } }; int main() { Time t1(2, 30), t2(3, 45), t3; t3 = t1 + t2; t3.display(); return 0; } // Write a ‘C++’ program to find factorial of given number using loop. #include <iostream> using namespace std; int main() { int n, i; long long factorial = 1; cout << "Enter a positive integer: "; cin >> n; if (n < 0) cout << "Error! Factorial of a negative number doesn't exist."; else { for(i = 1; i <= n; ++i) { factorial *= i; } cout << "Factorial of " << n << " = " << factorial; } return 0; } // Write a C++ program to declare a class COLLEGE with members as college // code. Derive a new class as STUDENT with members as stud_id. Accept // and display details of student along with college for one object of // student. #include <iostream> #include <stdlib.h> #include <string> #include <conio.h> using namespace std; class College { protected: int collegeCode; public: void getCollegeCode() { cout << "Enter college code: "; cin >> collegeCode; } void displayCollegeCode() { cout << "College code: " << collegeCode << endl; } }; class Student : public College { int rollNo; public: void getDetails() { getCollegeCode(); cout << "Enter roll number: "; cin >> rollNo; } void displayDetails() { displayCollegeCode(); cout << "Roll number: " << rollNo << endl; } }; int main() { Student s; s.getDetails(); s.displayDetails(); return 0; } // Write a C++ program to find smallest number from two numbers using // friend function. (Hint : use two classes). #include <iostream> using namespace std; class Number { private: int a, b; public: void getNumber() { cout << "Enter two numbers: "; cin >> a >> b; } friend int smallest(Number); }; int smallest(Number n) { if (n.a < n.b) return n.a; else return n.b; } int main() { Number n; n.getNumber(); cout << "Smallest number = " << smallest(n); return 0; } // Write a program to implement multiple inheritance as shown in // following Figure No. 1: #include <iostream> #include <stdlib.h> #include <string> #include <conio.h> using namespace std; class Subject1 { public: float m1; void getdata() { cout<<"Enter marks of subject 1: "; cin>>m1; } void showdata() { cout<<"Marks of subject 1: "<<m1<<endl; } }; class Subject2 { public: float m2; void getdata() { cout<<"Enter marks of subject 2: "; cin>>m2; } void showdata() { cout<<"Marks of subject 2: "<<m2<<endl; } }; class Result : public Subject1, public Subject2 { float total; public: void getdata() { Subject1::getdata(); Subject2::getdata(); } void showdata() { Subject1::showdata(); Subject2::showdata(); total = Subject1::m1 + Subject2::m2; cout<<"Total marks: "<<total<<endl; } }; int main() { Result r; r.getdata(); r.showdata(); return 0; } // Write a C++ program to add two 3 × 3 matrices and display addition. #include <iostream> #include <stdlib.h> #include <string> #include <conio.h> using namespace std; int main() { int m1[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; int m2[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; int m3[3][3]; int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { m3[i][j] = m1[i][j] + m2[i][j]; } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<m3[i][j]<<" "; } cout<<endl; } return 0; } // Write a program to swap two integers using call by reference method. #include <iostream> using namespace std; void swap(int &a, int &b) { int temp; temp = a; a = b; b = temp; } int main() { int a = 10, b = 20; cout << "Before swapping a = " << a << " and b = " << b << endl; swap(a, b); cout << "After swapping a = " << a << " and b = " << b << endl; return 0; } // Write a C++ program to find and display the smallest number of an array. #include <iostream> using namespace std; int main() { int a[10], i, small; cout << "Enter 10 numbers: "; for (i = 0; i < 10; i++) { cin >> a[i]; } small = a[0]; for (i = 0; i < 10; i++) { if (a[i] < small) { small = a[i]; } } cout << "Smallest number is: " << small << endl; return 0; } // Write a C++ program to declare a structure book with members as book // id and name. Accept and display data of five books using array of // structure. #include <iostream> #include <stdlib.h> #include <string> #include <conio.h> using namespace std; struct Book { int book_id; string name; }; int main() { Book b[5]; int i; for (i = 0; i < 5; i++) { cout << "Enter book id: "; cin >> b[i].book_id; cout << "Enter book name: "; cin >> b[i].name; } for (i = 0; i < 5; i++) { cout << "Book id: " << b[i].book_id << endl; cout << "Book name: " << b[i].name << endl; } return 0; }