cpp
- Get link
- X
- Other Apps
Assignment 1 Q1. Write a menu driven program for Date in a C. Declare a structure Date having data members day, month, year. Implement the following functions. void initDate(struct Date* ptrDate); void printDateOnConsole(struct Date* ptrDate); void acceptDateFromConsole(struct Date* ptrDate); Q2. Write a menu driven program for Date in a CPP language using structure and also using class. Date is having data members day, month, year. Implement the following functions. void initDate(); void printDateOnConsole(); void acceptDateFromConsole(); bool isLeapYear(); Q3. Write a menu driven program for Student in CPP language. Create a class student with data members roll no, name and marks. Implement the following functions void initStudent(); void printStudentOnConsole(); void acceptStudentFromConsole(); Q4. Create a namespace NStudent. Create the Student class(created as per Q3) inside namespace. Create the object of student and perform accept and display student. in c++
I'd be happy to help you with these assignments. Here are the solutions:
Q1. C Program for Date using Structure
Here is the menu-driven program for Date in C using a structure:
c1#include <stdio.h> 2 3struct Date { 4 int day; 5 int month; 6 int year; 7}; 8 9void initDate(struct Date* ptrDate) { 10 ptrDate->day = 0; 11 ptrDate->month = 0; 12 ptrDate->year = 0; 13} 14 15void printDateOnConsole(struct Date* ptrDate) { 16 printf("%d/%d/%d\n", ptrDate->day, ptrDate->month, ptrDate->year); 17} 18 19void acceptDateFromConsole(struct Date* ptrDate) { 20 printf("Enter day: "); 21 scanf("%d", &ptrDate->day); 22 printf("Enter month: "); 23 scanf("%d", &ptrDate->month); 24 printf("Enter year: "); 25 scanf("%d", &ptrDate->year); 26} 27 28int main() { 29 struct Date date; 30 int choice; 31 32 while (1) { 33 printf("Menu:\n"); 34 printf("1. Initialize Date\n"); 35 printf("2. Print Date\n"); 36 printf("3. Accept Date\n"); 37 printf("4. Exit\n"); 38 printf("Enter your choice: "); 39 scanf("%d", &choice); 40 41 switch (choice) { 42 case 1: 43 initDate(&date); 44 break; 45 case 2: 46 printDateOnConsole(&date); 47 break; 48 case 3: 49 acceptDateFromConsole(&date); 50 break; 51 case 4: 52 return 0; 53 default: 54 printf("Invalid choice\n"); 55 } 56 } 57 58 return 0; 59}
Q2. C++ Program for Date using Structure and Class
Here are the menu-driven programs for Date in C++ using a structure and a class:
Using Structure:
cpp1#include <iostream> 2 3struct Date { 4 int day; 5 int month; 6 int year; 7}; 8 9void initDate(struct Date& date) { 10 date.day = 0; 11 date.month = 0; 12 date.year = 0; 13} 14 15void printDateOnConsole(const struct Date& date) { 16 std::cout << date.day << "/" << date.month << "/" << date.year << std::endl; 17} 18 19void acceptDateFromConsole(struct Date& date) { 20 std::cout << "Enter day: "; 21 std::cin >> date.day; 22 std::cout << "Enter month: "; 23 std::cin >> date.month; 24 std::cout << "Enter year: "; 25 std::cin >> date.year; 26} 27 28bool isLeapYear(const struct Date& date) { 29 if (date.year % 4 == 0 && date.year % 100 != 0 || date.year % 400 == 0) 30 return true; 31 return false; 32} 33 34int main() { 35 struct Date date; 36 int choice; 37 38 while (1) { 39 std::cout << "Menu:\n"; 40 std::cout << "1. Initialize Date\n"; 41 std::cout << "2. Print Date\n"; 42 std::cout << "3. Accept Date\n"; 43 std::cout << "4. Check Leap Year\n"; 44 std::cout << "5. Exit\n"; 45 std::cout << "Enter your choice: "; 46 std::cin >> choice; 47 48 switch (choice) { 49 case 1: 50 initDate(date); 51 break; 52 case 2: 53 printDateOnConsole(date); 54 break; 55 case 3: 56 acceptDateFromConsole(date); 57 break; 58 case 4: 59 if (isLeapYear(date)) 60 std::cout << "Leap Year" << std::endl; 61 else 62 std::cout << "Not a Leap Year" << std::endl; 63 break; 64 case 5: 65 return 0; 66 default: 67 std::cout << "Invalid choice" << std::endl; 68 } 69 } 70 71 return 0; 72}
Using Class:
cpp1#include <iostream> 2 3class Date { 4private: 5 int day; 6 int month; 7 int year; 8public: 9 void initDate() { 10 day = 0; 11 month = 0; 12 year = 0; 13 } 14 15 void printDateOnConsole() const { 16 std::cout << day << "/" << month << "/" << year << std::endl; 17 } 18 19 void acceptDateFromConsole() { 20 std::cout << "Enter day: "; 21 std::cin >> day; 22 std::cout << "Enter month: "; 23 std::cin >> month; 24 std::cout << "Enter year: "; 25 std::cin >> year; 26 } 27 28 bool isLeapYear() const { 29 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) 30 return true; 31 return false; 32 } 33}; 34 35int main() { 36 Date date; 37 int choice; 38 39 while (1) { 40 std::cout << "Menu:\n"; 41 std::cout << "1. Initialize Date\n"; 42 std::cout << "2. Print Date\n"; 43 std::cout << "3. Accept Date\n"; 44 std::cout << "4. Check Leap Year\n"; 45 std::cout << "5. Exit\n"; 46 std::cout << "Enter your choice: "; 47 std::cin >> choice; 48 49 switch (choice) { 50 case 1: 51 date.initDate(); 52 break; 53 case 2: 54 date.printDateOnConsole(); 55 break; 56 case 3: 57 date.acceptDateFromConsole(); 58 break; 59 case 4: 60 if (date.isLeapYear()) 61 std::cout << "Leap Year" << std::endl; 62 else 63 std::cout << "Not a Leap Year" << std::endl; 64 break; 65 case 5: 66 return 0; 67 default: 68 std::cout << "Invalid choice" << std::endl; 69 } 70 } 71 72 return 0; 73}
Q3. C++ Program for Student
Here is the menu-driven program for Student in C++:
cpp1#include <iostream> 2#include <string> 3 4class Student { 5private: 6 int rollNo; 7 std::string name; 8 float marks; 9public: 10 void initStudent() { 11 rollNo = 0; 12 name = ""; 13 marks = 0.0; 14 } 15 16 void printStudentOnConsole() const { 17 std::cout << "Roll No: " << rollNo << std::endl; 18 std::cout << "Name: " << name << std::endl; 19 std::cout << "Marks: " << marks << std::endl; 20 } 21 22 void acceptStudentFromConsole() { 23 std::cout << "Enter roll no: "; 24 std::cin >> rollNo; 25 std::cout << "Enter name: "; 26 std::cin.ignore(); 27 std::getline(std::cin, name); 28 std::cout << "Enter marks: "; 29 std::cin >> marks; 30 } 31}; 32 33int main() { 34 Student student; 35 int choice; 36 37 while (1) { 38 std::cout << "Menu:\n";
Assignment 2
Q1. Write a menu driven program to calculate volume of the box(length * width * height). Provide parameterless, parameterized(with 3 parameters) and single paramaterized constructor. Create the local objects in respective case and call the function to caluclate area. Menu options ->
- Calculate Volume with default values
- Calculate Volume with length,breadth and height with same value
- Calculate Volume with different length,breadth and height values.
Q2. Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a fifty-cent toll. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps track of the number of cars that have gone by and of the total amount of money collected. Model this tollbooth with a class called tollbooth. The two data items are a type unsigned int to hold the total number of cars, and a type double to hold the total amount of money collected. A constructor initializes all data members to 0. A member function called payingCar() increments the car total and adds 0.50 to the cash total. An other function, called nopayCar() increments the car total but adds nothing to the cash total. Finally, a member function called printOnConsole() displays the two totals and number of paying as well as non paying cars total.
Q3. Write a class Address with data members (string building, string street, string city ,int pin) Implement constructors, getters, setters, accept(), and display() methods. Test the class functunalities by creating the object of class and calling all the functions.
I'd be happy to help you with these assignments. Here are the solutions:
Q1. C++ Program for Box
Here is the menu-driven program for Box in C++:
cpp1#include <iostream> 2 3class Box { 4private: 5 double length; 6 double width; 7 double height; 8public: 9 // Parameterless constructor 10 Box() : length(1.0), width(1.0), height(1.0) {} 11 12 // Parameterized constructor with 3 parameters 13 Box(double l, double w, double h) : length(l), width(w), height(h) {} 14 15 // Single parameterized constructor 16 Box(double side) : length(side), width(side), height(side) {} 17 18 double calculateVolume() const { 19 return length * width * height; 20 } 21}; 22 23int main() { 24 int choice; 25 26 while (1) { 27 std::cout << "Menu:\n"; 28 std::cout << "1. Calculate Volume with default values\n"; 29 std::cout << "2. Calculate Volume with length, breadth, and height with same value\n"; 30 std::cout << "3. Calculate Volume with different length, breadth, and height values\n"; 31 std::cout << "4. Exit\n"; 32 std::cout << "Enter your choice: "; 33 std::cin >> choice; 34 35 switch (choice) { 36 case 1: { 37 Box box; 38 std::cout << "Volume: " << box.calculateVolume() << std::endl; 39 break; 40 } 41 case 2: { 42 double side; 43 std::cout << "Enter side: "; 44 std::cin >> side; 45 Box box(side); 46 std::cout << "Volume: " << box.calculateVolume() << std::endl; 47 break; 48 } 49 case 3: { 50 double length, width, height; 51 std::cout << "Enter length: "; 52 std::cin >> length; 53 std::cout << "Enter width: "; 54 std::cin >> width; 55 std::cout << "Enter height: "; 56 std::cin >> height; 57 Box box(length, width, height); 58 std::cout << "Volume: " << box.calculateVolume() << std::endl; 59 break; 60 } 61 case 4: 62 return 0; 63 default: 64 std::cout << "Invalid choice" << std::endl; 65 } 66 } 67 68 return 0; 69}
Q2. C++ Program for Tollbooth
Here is the menu-driven program for Tollbooth in C++:
cpp1#include <iostream> 2 3class Tollbooth { 4private: 5 unsigned int totalCars; 6 double totalMoney; 7public: 8 Tollbooth() : totalCars(0), totalMoney(0.0) {} 9 10 void payingCar() { 11 totalCars++; 12 totalMoney += 0.50; 13 } 14 15 void nopayCar() { 16 totalCars++; 17 } 18 19 void printOnConsole() const { 20 std::cout << "Total Cars: " << totalCars << std::endl; 21 std::cout << "Total Money: $" << totalMoney << std::endl; 22 std::cout << "Paying Cars: " << static_cast<int>(totalMoney / 0.50) << std::endl; 23 std::cout << "Non-Paying Cars: " << totalCars - static_cast<int>(totalMoney / 0.50) << std::endl; 24 } 25}; 26 27int main() { 28 Tollbooth tollbooth; 29 int choice; 30 31 while (1) { 32 std::cout << "Menu:\n"; 33 std::cout << "1. Paying Car\n"; 34 std::cout << "2. Non-Paying Car\n"; 35 std::cout << "3. Print Totals\n"; 36 std::cout << "4. Exit\n"; 37 std::cout << "Enter your choice: "; 38 std::cin >> choice; 39 40 switch (choice) { 41 case 1: 42 tollbooth.payingCar(); 43 break; 44 case 2: 45 tollbooth.nopayCar(); 46 break; 47 case 3: 48 tollbooth.printOnConsole(); 49 break; 50 case 4: 51 return 0; 52 default: 53 std::cout << "Invalid choice" << std::endl; 54 } 55 } 56 57 return 0; 58}
Q3. C++ Program for Address
Here is the menu-driven program for Address in C++:
cpp1#include <iostream> 2#include <string> 3 4class Address { 5private: 6 std::string building; 7 std::string street; 8 std::string city; 9 int pin; 10public: 11 Address() : building(""), street(""), city(""), pin(0) {} 12 13 void accept() { 14 std::cout << "Enter building: "; 15 std::cin.ignore(); 16 std::getline(std::cin, building); 17 std::cout << "Enter street: "; 18 std::getline(std::cin, street); 19 std::cout << "Enter city: "; 20 std::getline(std::cin, city); 21 std::cout << "Enter pin: "; 22 std::cin >> pin; 23 } 24 25 void display() const { 26 std::cout << "Building: " << building << std::endl; 27 std::cout << "Street: " << street << std::endl; 28 std::cout << "City: " << city << std::endl; 29 std::cout << "Pin: " << pin << std::endl; 30 } 31 32 std::string getBuilding() const { 33 return building; 34 } 35 36 void setBuilding(const std::string& b) { 37 building = b; 38 } 39 40 std::string getStreet() const { 41 return street; 42 } 43 44 void setStreet(const std::string& s) { 45 street = s; 46 } 47 48 std::string getCity() const { 49 return city; 50 } 51 52 void setCity(const std::string& c) { 53 city = c; 54 } 55 56 int getPin() const { 57 return pin; 58 } 59 60 void setPin(int p) { 61 pin = p; 62 } 63}; 64 65int main() { 66 Address address; 67 68 address.accept(); 69 address.display(); 70 71 return 0; 72}
Let me know if you have any questions or need further clarification on any of these assignments!
Q1. Create a class Date with data memebrs day,month and year. Cretae a class Person with data members name,address and birthdate. The birthdate shoud be of type Date. Implement above classes. Test all functionalities in main().\
Q2. Write a menu driven program for Student management. Create a class student with data members name, gender, rollNumber(Auto generated) and array to keep marks of three subjects.
Accept every thing from user and Print name, rollNumber, gender and percentage. Provide global functions void sortRecords() and int searchRecords() for sorting and searching array. In main(), create Student* arr[5] and provide facility for accept, print, search and sort. Search function returns index of found Student, otherwise returns -1. sortRecord sorts the students array based on roll no in descending order.
I'd be happy to help you with these assignments. Here are the solutions:
Q1. C++ Program for Date and Person
Here is the implementation of the Date and Person classes in C++:
cpp1#include <iostream> 2#include <string> 3 4class Date { 5private: 6 int day; 7 int month; 8 int year; 9public: 10 Date(int d = 1, int m = 1, int y = 1900) : day(d), month(m), year(y) {} 11 12 void setDay(int d) { 13 day = d; 14 } 15 16 void setMonth(int m) { 17 month = m; 18 } 19 20 void setYear(int y) { 21 year = y; 22 } 23 24 int getDay() const { 25 return day; 26 } 27 28 int getMonth() const { 29 return month; 30 } 31 32 int getYear() const { 33 return year; 34 } 35}; 36 37class Person { 38private: 39 std::string name; 40 std::string address; 41 Date birthdate; 42public: 43 Person(const std::string& n, const std::string& a, int d, int m, int y) 44 : name(n), address(a), birthdate(d, m, y) {} 45 46 void setName(const std::string& n) { 47 name = n; 48 } 49 50 void setAddress(const std::string& a) { 51 address = a; 52 } 53 54 void setBirthdate(int d, int m, int y) { 55 birthdate.setDay(d); 56 birthdate.setMonth(m); 57 birthdate.setYear(y); 58 } 59 60 std::string getName() const { 61 return name; 62 } 63 64 std::string getAddress() const { 65 return address; 66 } 67 68 Date getBirthdate() const { 69 return birthdate; 70 } 71}; 72 73int main() { 74 Person person("John Doe", "123 Main St", 12, 5, 1990); 75 76 std::cout << "Name: " << person.getName() << std::endl; 77 std::cout << "Address: " << person.getAddress() << std::endl; 78 std::cout << "Birthdate: " << person.getBirthdate().getDay() << "/" 79 << person.getBirthdate().getMonth() << "/" 80 << person.getBirthdate().getYear() << std::endl; 81 82 return 0; 83}
Q2. C++ Program for Student Management
Here is the implementation of the Student class and the menu-driven program for student management in C++:
cpp1#include <iostream> 2#include <string> 3 4class Student { 5private: 6 std::string name; 7 char gender; 8 int rollNumber; 9 int marks[3]; 10public: 11 static int rollNumberCounter; 12 13 Student(const std::string& n, char g, int m1, int m2, int m3) 14 : name(n), gender(g), rollNumber(++rollNumberCounter), marks{m1, m2, m3} {} 15 16 void setName(const std::string& n) { 17 name = n; 18 } 19 20 void setGender(char g) { 21 gender = g; 22 } 23 24 void setMarks(int m1, int m2, int m3) { 25 marks[0] = m1; 26 marks[1] = m2; 27 marks[2] = m3; 28 } 29 30 std::string getName() const { 31 return name; 32 } 33 34 char getGender() const { 35 return gender; 36 } 37 38 int getRollNumber() const { 39 return rollNumber; 40 } 41 42 int getMarks(int index) const { 43 return marks[index]; 44 } 45 46 double getPercentage() const { 47 return (marks[0] + marks[1] + marks[2]) / 3.0; 48 } 49}; 50 51int Student::rollNumberCounter = 0; 52 53void sortRecords(Student* arr[], int size) { 54 for (int i = 0; i < size - 1; i++) { 55 for (int j = i + 1; j < size; j++) { 56 if (arr[i]->getRollNumber() < arr[j]->getRollNumber()) { 57 Student* temp = arr[i]; 58 arr[i] = arr[j]; 59 arr[j] = temp; 60 } 61 } 62 } 63} 64 65int searchRecords(Student* arr[], int size, int rollNumber) { 66 for (int i = 0; i < size; i++) { 67 if (arr[i]->getRollNumber() == rollNumber) { 68 return i; 69 } 70 } 71 return -1; 72} 73 74int main() { 75 Student* arr[5]; 76 77 int choice; 78 while (1) { 79 std::cout << "Menu:\n"; 80 std::cout << "1. Accept Student Details\n"; 81 std::cout << "2. Print Student Details\n"; 82 std::cout << "3. Search Student by Roll Number\n"; 83 std::cout << "4. Sort Students by Roll Number\n"; 84 std::cout << "5. Exit\n"; 85 std::cout << "Enter your choice: "; 86 std::cin >> choice; 87 88 switch (choice) { 89 case 1: { 90 std::string name; 91 char gender; 92 int marks[3]; 93 94 std::cout << "Enter name: "; 95 std::cin.ignore(); 96 std::getline(std::cin, name); 97 std::cout << "Enter gender: "; 98 std::cin >> gender; 99 std::cout << "Enter marks for subject 1: "; 100 std::cin >> marks[0]; 101 std::cout << "Enter marks for subject 2: "; 102 std::cin >> marks[1]; 103 std::cout << "Enter marks for subject 3: "; 104 std::cin >> marks[2]; 105 106 arr[Student::rollNumberCounter] = new Student(name, gender, marks[0], marks[1], marks[2]); 107 Student::rollNumberCounter++; 108 break; 109 } 110 case 2: { 111 for (int i = 0; i < Student::rollNumberCounter; i++) { 112 std::cout << "Name: " << arr[i]->getName() << std::endl; 113 std::cout << "Roll Number: " << arr[i]->getRollNumber() << std::endl; 114 std::cout << "Gender: " << arr[i]->getGender() << std::endl; 115 std::cout << "Percentage: " << arr[i]->getPercentage() << std::endl; 116 std::cout << std::endl; 117 } 118 break; 119 } 120 case 3: { 121 int rollNumber; 122 std::cout << "Enter roll number to search: "; 123 std::cin >> rollNumber; 124 125 int index = searchRecords(arr, Student::rollNumberCounter, rollNumber); 126 if (index != -1) { 127 std::cout << "Name: " << arr[index]->getName() << std::endl; 128 std::cout << "Roll Number: " << arr[index]->getRollNumber() << std::endl; 129 std::cout << "Gender: " << arr[index]->getGender() << std::endl; 130 std::cout << "Percentage: " << arr[index]->getPercentage() << std::endl; 131 } else { 132 std::cout << "Student not found"
Assignment 5 Q1. Create a class Date with data memebrs day,month and year. Cretae a class Person with data members name,address and birthdate. The birthdate shoud be of type Date. Cretae a class Employee with data members id, sal, dept and joining date. The joining date should be of type date. Employee class should be inherited from Person. Implement following classes. Test all functionalities in main(). (Note - Perform the Association and Inheritance in the above case.) Q2. A shop sells book or tapes. The Book class holds id, title, author, and price; whereas Tape class holds id, title, artist, and price. There is 5% discount on tapes, while 10% discount on books. Assuming that each user purchase 3 products (either book or tape), calculate final bill. The program should be menu driven and should not cause memory leakage. Hint - Create class Product and inherit into Book and Tape. Also create array like Product* arr[3] Q3. Implement following classes. Test all functionalities in main() of each created class. Note that employee is inherited into manager and salesman.
I'd be happy to help you with these assignments. Here are the solutions:
Q1. C++ Program for Date, Person, and Employee
Here is the implementation of the Date, Person, and Employee classes in C++:
cpp1#include <iostream> 2#include <string> 3 4class Date { 5private: 6 int day; 7 int month; 8 int year; 9public: 10 Date(int d = 1, int m = 1, int y = 1900) : day(d), month(m), year(y) {} 11 12 void setDay(int d) { 13 day = d; 14 } 15 16 void setMonth(int m) { 17 month = m; 18 } 19 20 void setYear(int y) { 21 year = y; 22 } 23 24 int getDay() const { 25 return day; 26 } 27 28 int getMonth() const { 29 return month; 30 } 31 32 int getYear() const { 33 return year; 34 } 35}; 36 37class Person { 38private: 39 std::string name; 40 std::string address; 41 Date birthdate; 42public: 43 Person(const std::string& n, const std::string& a, int d, int m, int y) 44 : name(n), address(a), birthdate(d, m, y) {} 45 46 void setName(const std::string& n) { 47 name = n; 48 } 49 50 void setAddress(const std::string& a) { 51 address = a; 52 } 53 54 void setBirthdate(int d, int m, int y) { 55 birthdate.setDay(d); 56 birthdate.setMonth(m); 57 birthdate.setYear(y); 58 } 59 60 std::string getName() const { 61 return name; 62 } 63 64 std::string getAddress() const { 65 return address; 66 } 67 68 Date getBirthdate() const { 69 return birthdate; 70 } 71}; 72 73class Employee : public Person { 74private: 75 int id; 76 double sal; 77 std::string dept; 78 Date joiningDate; 79public: 80 Employee(int i, double s, const std::string& d, int jd, int jm, int jy, const std::string& n, const std::string& a, int bd, int bm, int by) 81 : Person(n, a, bd, bm, by), id(i), sal(s), dept(d), joiningDate(jd, jm, jy) {} 82 83 void setId(int i) { 84 id = i; 85 } 86 87 void setSal(double s) { 88 sal = s; 89 } 90 91 void setDept(const std::string& d) { 92 dept = d; 93 } 94 95 void setJoiningDate(int jd, int jm, int jy) { 96 joiningDate.setDay(jd); 97 joiningDate.setMonth(jm); 98 joiningDate.setYear(jy); 99 } 100 101 int getId() const { 102 return id; 103 } 104 105 double getSal() const { 106 return sal; 107 } 108 109 std::string getDept() const { 110 return dept; 111 } 112 113 Date getJoiningDate() const { 114 return joiningDate; 115 } 116}; 117 118int main() { 119 Employee employee(1, 5000.0, "HR", 12, 5, 2020, "John Doe", "123 Main St", 12, 5, 1990); 120 121 std::cout << "Name: " << employee.getName() << std::endl; 122 std::cout << "Address: " << employee.getAddress() << std::endl; 123 std::cout << "Birthdate: " << employee.getBirthdate().getDay() << "/" 124 << employee.getBirthdate().getMonth() << "/" 125 << employee.getBirthdate().getYear() << std::endl; 126 std::cout << "ID: " << employee.getId() << std::endl; 127 std::cout << "Salary: " << employee.getSal() << std::endl; 128 std::cout << "Department: " << employee.getDept() << std::endl; 129 std::cout << "Joining Date: " << employee.getJoiningDate().getDay() << "/" 130 << employee.getJoiningDate().getMonth() << "/" 131 << employee.getJoiningDate().getYear() << std::endl; 132 133 return 0; 134}
Q2. C++ Program for Book and Tape
Here is the implementation of the Book and Tape classes in C++:
cpp1#include <iostream> 2#include <string> 3 4class Product { 5private: 6 int id; 7 std::string title; 8 double price; 9public: 10 Product(int i, const std::string& t, double p) : id(i), title(t), price(p) {} 11 12 void setId(int i) { 13 id = i; 14 } 15 16 void setTitle(const std::string& t) { 17 title = t; 18 } 19 20 void setPrice(double p) { 21 price = p; 22 } 23 24 int getId() const { 25 return id; 26 } 27 28 std::string getTitle() const { 29 return title; 30 } 31 32 double getPrice() const { 33 return price; 34 } 35 36 virtual double getDiscount() const = 0; 37}; 38 39class Book : public Product { 40private: 41 std::string author; 42public: 43 Book(int i, const std::string& t, double p, const std::string& a) 44 : Product(i, t, p), author(a) {} 45 46 void setAuthor(const std::string& a) { 47 author = a; 48 } 49 50 std::string getAuthor() const { 51 return author; 52 } 53 54 double getDiscount() const override { 55 return 0.1; 56 } 57}; 58 59class Tape : public Product { 60private: 61 std::string artist; 62public: 63 Tape(int i, const std::string& t, double p, const std::string& a) 64 : Product(i, t, p), artist(a) {} 65 66 void setArtist(const std::string& a) { 67 artist = a; 68 } 69 70 std::string getArtist() const { 71 return artist; 72 } 73 74 double getDiscount() const override { 75 return 0.05; 76 } 77}; 78 79int main() { 80 Product* arr[3]; 81 82 int choice; 83 while (1) { 84 std::cout << "Menu:\n"; 85 std::cout << "1. Add Book\n"; 86 std::cout << "2. Add Tape\n"; 87 std::cout << "3. Calculate Bill\n"; 88 std::cout << "4. Exit\n"; 89 std::cout << "Enter your choice: "; 90 std::cin >> choice; 91 92 switch (choice) { 93 case 1: { 94 int id; 95 std::string title; 96 double price; 97 std::string author; 98 99 std::cout << "Enter id: "; 100 std::cin >> id; 101 std::cout << "Enter title: "; 102 std::cin.ignore(); 103 std::getline(std::cin, title); 104 std::cout << "Enter price: "; 105 std::cin >> price; 106 std::cout << "Enter author: "; 107 std::cin.ignore(); 108 std::getline(std::cin, author); 109 110 arr[0] = new Book(id, title, price, author); 111 break; 112 } 113 case 2: { 114 int id; 115 std::string title; 116 double price; 117 std::string artist; 118 119 std::
- Get link
- X
- Other Apps
Comments
Post a Comment