├── .DS_Store ├── .gitattributes ├── class-objects ├── average-population.cpp ├── bank-project.cpp ├── complex-numbers.cpp ├── device-class.cpp ├── gradebook-class.cpp ├── health-profile-class.cpp ├── number-class.cpp ├── occurance-class.cpp ├── student-class.cpp ├── student-details.cpp ├── type-of-triangle.cpp └── vehicle-class.cpp ├── error-handling ├── catch-try.cpp ├── error-handling.cpp └── try-catch-throw.cpp ├── methods-properties ├── case-toggling.cpp ├── circle-properties.cpp ├── conditional-operator.cpp ├── conversion-kg.cpp ├── first-n-prime.cpp ├── input-display.cpp ├── spiral-pattern.cpp ├── string-musicplayer.cpp ├── taxi-fare.cpp └── unique-integer.cpp ├── polymorphism-friend ├── abstract-details.cpp ├── constructor-overloading.cpp ├── function-overloading.cpp ├── new-oper-overloading.cpp ├── operator-overloading.cpp └── price-function-overloading.cpp ├── templates-standard-library ├── class-template.cpp ├── currency-converter.cpp ├── function-template.cpp ├── search-function.cpp ├── sort-stl.cpp └── vector-stl.cpp └── types-of-inheritance ├── GPA-multiple-inheritance.cpp ├── Parent-inheritance.cpp ├── appetizer-menu-maincourse.cpp ├── cart-kitchen-store.cpp ├── fruit-inheritance.cpp ├── hirearichal-inheritance.cpp ├── hybrid-inheritance.cpp ├── multilevel-inheritance.cpp └── multiple-inheritance.cpp /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nishant-Tiwari24/object-oriented-programming/85a217a151ce49632a4c93608a765c7a5ba6cd67/.DS_Store -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /class-objects/average-population.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | struct pop{ 7 | string str; 8 | int n; 9 | struct pop *next; 10 | }; 11 | 12 | int z; 13 | int *e=&z; 14 | 15 | int main() { 16 | int a=3; 17 | struct pop *temp,*head=0,*ptr; 18 | for(int i=0;i>ptr->str; 21 | cin>>ptr->n; 22 | if(head==0) 23 | { 24 | head=temp=ptr; 25 | } 26 | else{ 27 | temp->next=ptr; 28 | temp=ptr; 29 | } 30 | } 31 | float b=0; 32 | temp=head; 33 | for(int i=0;in; 35 | temp=temp->next; 36 | } 37 | cout< 2 | 3 | class BankAccount { 4 | private: 5 | int balance; 6 | 7 | public: 8 | BankAccount() : balance(0) {} 9 | 10 | void deposit(int amount) { 11 | balance += amount; 12 | std::cout << "Deposit successful." << std::endl; 13 | } 14 | 15 | void withdraw(int amount) { 16 | if (amount <= balance) { 17 | balance -= amount; 18 | std::cout << "Withdrawal successful." << std::endl; 19 | } else { 20 | std::cout << "Insufficient balance." << std::endl; 21 | } 22 | } 23 | 24 | void displayBalance() { 25 | std::cout << "Current Balance: " << balance << std::endl; 26 | } 27 | }; 28 | 29 | int main() { 30 | BankAccount* account = new BankAccount(); 31 | 32 | while (true) { 33 | int choice; 34 | std::scanf("%d",&choice); 35 | 36 | if (choice == 1) { 37 | int amount; 38 | std::cin >> amount; 39 | account->deposit(amount); 40 | } else if (choice == 2) { 41 | int amount; 42 | std::cin >> amount; 43 | (*account).withdraw(amount); 44 | } else if (choice == 3) { 45 | account->displayBalance(); 46 | } else if (choice == 4) { 47 | std::cout << "Exiting program." << std::endl; 48 | delete account; 49 | break; 50 | } else { 51 | std::cout << "Invalid choice." << std::endl; 52 | } 53 | } 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /class-objects/complex-numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class complex { 4 | int a; 5 | int b; 6 | 7 | public: 8 | void setData(int a, int b) { 9 | a = a; 10 | b = b; 11 | } 12 | 13 | void getSum(complex o1, complex o2) { 14 | a = o1.a + o2.a; 15 | b = o1.b + o2.b; 16 | } 17 | }; 18 | 19 | int main() { 20 | complex c1, c2, c3; 21 | c2.setData(1,2); 22 | c3.setData(3,4); 23 | c1.getSum(c2, c3); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /class-objects/device-class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Device; 5 | 6 | class Device { 7 | private: 8 | int age; 9 | int resting_heart_rate_awake; 10 | 11 | public: 12 | Device(int a, int rhr_awake) : age(a), resting_heart_rate_awake(rhr_awake) {} 13 | 14 | friend void calculateHeartRates(Device& d); 15 | }; 16 | 17 | void calculateHeartRates(Device& d) { 18 | int mhr = 220 - d.age; 19 | int rhr_sleep = 0.9 * d.resting_heart_rate_awake; 20 | 21 | cout << "MHR: " << mhr << " bpm" << endl; 22 | cout << "RHR_sleep: " << rhr_sleep << " bpm" << endl; 23 | } 24 | 25 | int main() { 26 | int age, rhr_awake; 27 | cin >> age >> rhr_awake; 28 | Device device(age, rhr_awake); 29 | calculateHeartRates(device); 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /class-objects/gradebook-class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class GradeBook { 6 | private: 7 | string course_name; 8 | string instructor_name; 9 | 10 | public: 11 | void setData(const string& course, const string& instructor) { 12 | course_name = course; 13 | instructor_name = instructor; 14 | } 15 | 16 | void displayMessage() { 17 | cout << "Welcome to the grade book for " << course_name << "!" << endl; 18 | cout << "This course is presented by: " << instructor_name << endl; 19 | } 20 | }; 21 | 22 | int main() { 23 | string course_name, instructor_name; 24 | getline(cin, course_name); 25 | getline(cin, instructor_name); 26 | GradeBook gradeBook; 27 | gradeBook.setData(course_name, instructor_name); 28 | gradeBook.displayMessage(); 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /class-objects/health-profile-class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class HealthProfile { 6 | public: 7 | std::string name; 8 | int age; 9 | double height; 10 | double weight; 11 | 12 | HealthProfile(std::string n, int a, double h, double w) : name(n), age(a), height(h), weight(w) {} 13 | 14 | double calculateBMI() { 15 | return weight/(height * height); 16 | } 17 | 18 | void displayProfile() { 19 | double bmi = calculateBMI(); 20 | std::cout << "BMI: " << std::fixed << std::setprecision(2) << bmi << std::endl; 21 | } 22 | }; 23 | 24 | int main() { 25 | std::string name; 26 | int age; 27 | double height, weight; 28 | 29 | std::getline(std::cin, name); 30 | std::cin >> age >> height >> weight; 31 | HealthProfile profile(name, age, height, weight); 32 | profile.displayProfile(); 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /class-objects/number-class.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | class TimeDuration { 5 | public: 6 | int hours, minutes, seconds; 7 | 8 | TimeDuration(int h, int m, int s) : hours(h), minutes(m), seconds(s) {} 9 | 10 | void add(const TimeDuration& other) { 11 | seconds += other.seconds; 12 | minutes += seconds / 60; 13 | seconds %= 60; 14 | minutes += other.minutes; 15 | hours += minutes / 60; 16 | minutes %= 60; 17 | hours += other.hours; 18 | } 19 | 20 | void display() const { 21 | std::cout << "Time: " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds" << std::endl; 22 | } 23 | }; 24 | 25 | int main() { 26 | int h1, m1, s1, h2, m2, s2; 27 | std::cin >> h1 >> m1 >> s1; 28 | std::cin >> h2 >> m2 >> s2; 29 | 30 | TimeDuration duration1(h1, m1, s1); 31 | TimeDuration duration2(h2, m2, s2); 32 | 33 | duration1.add(duration2); 34 | duration1.display(); 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /class-objects/occurance-class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Occurrence { 5 | private: 6 | std::string str; 7 | char ch; 8 | 9 | public: 10 | void init(const std::string& s, char c) { 11 | str = s; 12 | ch = c; 13 | } 14 | void countOccurrence() { 15 | int count = 0; 16 | for (char character : str) { 17 | if (character == ch) { 18 | count++; 19 | } 20 | } 21 | std::cout << count << std::endl; 22 | } 23 | }; 24 | 25 | int main() { 26 | Occurrence occurrence; 27 | std::string s; 28 | char c; 29 | 30 | std::cin >> s; 31 | std::cin >> c; 32 | 33 | occurrence.init(s, c); 34 | occurrence.countOccurrence(); 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /class-objects/student-class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Student { 5 | public: 6 | int id; 7 | int age; 8 | std::string name; 9 | 10 | void inputStudentInfo() { 11 | std::cin >> id >> age; 12 | std::cin.ignore(); 13 | std::getline(std::cin, name); 14 | } 15 | void displayStudentInfo() { 16 | std::cout << "Name: " << name << std::endl; 17 | std::cout << "ID: " << id << std::endl; 18 | std::cout << "Age: " << age << std::endl; 19 | } 20 | }; 21 | 22 | int main() { 23 | Student student; 24 | student.inputStudentInfo(); 25 | student.displayStudentInfo(); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /class-objects/student-details.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Student { 5 | private: 6 | std::string name; 7 | int birth_year; 8 | 9 | public: 10 | 11 | Student() : name("XYZ"), birth_year(2023) { 12 | std::cout << "Name: " << name << std::endl; 13 | std::cout << "Year born: " << birth_year << std::endl; 14 | } 15 | Student(const std::string& student_name, int student_birth_year) : name(student_name), birth_year(student_birth_year) { 16 | std::cout << "Name: " << "XYZ" << std::endl; 17 | std::cout << "Year born: " << "2023" << std::endl; 18 | 19 | std::cout << "Name: " << name << std::endl; 20 | std::cout << "Year born: " << birth_year << std::endl; 21 | } 22 | ~Student() { 23 | std::cout << "Destroyed object: " << name << std::endl; 24 | } 25 | }; 26 | 27 | int main() { 28 | std::string name; 29 | int birth_year; 30 | std::getline(std::cin, name); 31 | std::cin >> birth_year; 32 | Student student(name, birth_year); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /class-objects/type-of-triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a, b, c; 6 | cin>>a>>b>>c; 7 | if(a==b && b==c &&c==a) { 8 | cout<<"Equilateral triangle"< 2 | #include 3 | 4 | class Vehicle { 5 | private: 6 | std::string registration_number; 7 | std::string make; 8 | std::string model; 9 | int year_of_manufacture; 10 | 11 | public: 12 | Vehicle(const std::string& reg_num, const std::string& vehicle_make, const std::string& vehicle_model, int year) { 13 | registration_number = reg_num; 14 | make = vehicle_make; 15 | model = vehicle_model; 16 | year_of_manufacture = year; 17 | } 18 | 19 | void displayDetails() { 20 | std::cout << "Registration Number: " << registration_number << std::endl; 21 | std::cout << "Make: " << make << std::endl; 22 | std::cout << "Model: " << model << std::endl; 23 | std::cout << "Year of Manufacture: " << year_of_manufacture << std::endl; 24 | } 25 | }; 26 | 27 | int main() { 28 | std::string reg_num, make, model; 29 | int year; 30 | std::getline(std::cin, reg_num); 31 | std::getline(std::cin, make); 32 | std::getline(std::cin, model); 33 | std::cin >> year; 34 | Vehicle vehicle(reg_num, make, model, year); 35 | vehicle.displayDetails(); 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /error-handling/catch-try.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class InvalidIDException : public std::exception { 6 | public: 7 | const char* what() const throw() { 8 | return "Invalid eyeglass ID. Eyeglass not added"; 9 | } 10 | }; 11 | 12 | class OutOfStockException : public std::exception { 13 | public: 14 | const char* what() const throw() { 15 | return "Eyeglass out of stock. Cannot add more eyeglasses"; 16 | } 17 | }; 18 | 19 | class OpticalShowroom { 20 | private: 21 | static const int MAX_STOCK = 50; 22 | std::vector stock; 23 | 24 | public: 25 | void addEyeglasses(const std::vector& eyeglasses) { 26 | try { 27 | for (int eyeglassID : eyeglasses) { 28 | if (stock.size() >= MAX_STOCK) { 29 | throw OutOfStockException(); 30 | } 31 | 32 | if (!(1 <= eyeglassID && eyeglassID <= 107)) { 33 | throw InvalidIDException(); 34 | } 35 | 36 | stock.push_back(eyeglassID); 37 | std::cout << "Eyeglass with ID " << eyeglassID << " added to the showroom\n"; 38 | } 39 | } catch (const InvalidIDException& e) { 40 | std::cout << "Exception caught. " << e.what() << std::endl; 41 | } catch (const OutOfStockException& e) { 42 | std::cout << "Exception caught. " << e.what() << std::endl; 43 | } 44 | } 45 | 46 | void sellEyeglass(int customerID) { 47 | try { 48 | auto it = std::find(stock.begin(), stock.end(), customerID); 49 | if (it == stock.end()) { 50 | throw InvalidIDException(); 51 | 52 | // std::cout << "Eyeglass with ID " << customerID << " sold\n"; 53 | } 54 | std::cout << "Eyeglass with ID " << customerID << " sold\n"; 55 | stock.erase(it); 56 | // std::cout << "Eyeglass with ID " << customerID << " sold\n"; 57 | } catch (const InvalidIDException& e) { 58 | std::cout << "Exception caught. " << e.what() << std::endl; 59 | } 60 | } 61 | 62 | }; 63 | 64 | int main() { 65 | int n; 66 | std::cin >> n; 67 | 68 | std::vector eyeglasses(n); 69 | for (int i = 0; i < n; ++i) { 70 | std::cin >> eyeglasses[i]; 71 | } 72 | 73 | int customerID; 74 | std::cin >> customerID; 75 | OpticalShowroom showroom; 76 | showroom.addEyeglasses(eyeglasses); 77 | showroom.sellEyeglass(customerID); 78 | 79 | return 0; 80 | } -------------------------------------------------------------------------------- /error-handling/error-handling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class InsufficientFundsException : public std::exception { 5 | public: 6 | const char* what() const throw() { 7 | return "Insufficient money for withdrawal"; 8 | } 9 | }; 10 | 11 | class BankAccount { 12 | private: 13 | double balance; 14 | 15 | public: 16 | BankAccount(double initialBalance) : balance(initialBalance) {} 17 | 18 | void withdraw(double amount) { 19 | if (amount > balance) { 20 | throw InsufficientFundsException(); 21 | } 22 | 23 | balance -= amount; 24 | } 25 | 26 | double getBalance() const { 27 | return balance; 28 | } 29 | }; 30 | 31 | int main() { 32 | double initialBalance, withdrawalAmount; 33 | std::cin >> initialBalance >> withdrawalAmount; 34 | 35 | BankAccount account(initialBalance); 36 | 37 | try { 38 | account.withdraw(withdrawalAmount); 39 | std::cout << "Withdrawal successful" << std::endl; 40 | std::cout << "New balance: " << std::fixed << std::setprecision(2) << account.getBalance() << std::endl; 41 | } catch (const InsufficientFundsException& e) { 42 | std::cout << "Exception caught: Error: " << e.what() << std::endl; 43 | } 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /error-handling/try-catch-throw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double calculate_result(int index, double numerator, double denominator) { 5 | try { 6 | double results[4] = {0.0}; 7 | if (index < 0 || index >= 4) { 8 | std::cout<<"Error: Array out of bounds!"<> index; 28 | 29 | double numerator, denominator; 30 | std::cin >> numerator >> denominator; 31 | 32 | // Check if the denominator is greater than zero 33 | if (denominator <= 0.00) { 34 | throw std::invalid_argument("Error: Denominator must be greater than 0"); 35 | } 36 | 37 | // Output the result or error message 38 | std::cout << calculate_result(index, numerator, denominator) << std::endl; 39 | 40 | } catch (const std::invalid_argument& e) { 41 | std::cerr << e.what() << std::endl; 42 | } 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /methods-properties/case-toggling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | string str; 7 | getline(cin,str); 8 | for(int i=0;i='A' && str[i]<='Z') 11 | str[i]=tolower(str[i]); 12 | else if(str[i]>='a' && str[i]<='z') 13 | str[i]=toupper(str[i]); 14 | } 15 | cout<<"Toggled string: "< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | double getDiameter(double radius) { 7 | float diameter = 2*radius; 8 | return diameter; 9 | } 10 | 11 | double getCircumfrence(double radius) { 12 | float circumfrence = 2*(M_PI)*radius; 13 | return circumfrence; 14 | } 15 | 16 | double getArea(double radius) { 17 | float area = (M_PI)*radius*radius; 18 | return area; 19 | } 20 | 21 | int main() { 22 | float r; 23 | cin>>r; 24 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | cin>>n; 8 | string result = (n%2==0) ? "Even" : "Odd"; 9 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | cin>>n; 8 | float ans = static_cast(n); 9 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int prime(int n) 6 | { 7 | if(n<=1) 8 | { 9 | return false; 10 | } 11 | for(int i=2;i<=n/2;i++) 12 | { 13 | if(n%i==0) 14 | { 15 | return false; 16 | } 17 | } 18 | return true; 19 | } 20 | 21 | int main() 22 | { 23 | int n; 24 | cin>>n; 25 | int i=2; 26 | while(n!=0) 27 | { 28 | if(prime(i)) 29 | { 30 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | struct input { 8 | string name; 9 | int weight; 10 | }; 11 | 12 | bool compInput(const input &a, const input &b) { 13 | return a.weight < b.weight; 14 | } 15 | 16 | int main() { 17 | int n; 18 | cin>>n; 19 | vector inputs(n); 20 | for(int i=0; i>inputs[i].name>>inputs[i].weight; 22 | } 23 | 24 | sort(inputs.begin(),inputs.end(),compInput); 25 | 26 | for(const input &p: inputs) { 27 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | int size = 2*n-1; 9 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | vectorstrings; 11 | int n; 12 | cin>>n; 13 | string str; 14 | while(n--) 15 | { 16 | cin>>str; 17 | strings.push_back(str); 18 | } 19 | sort(strings.begin(),strings.end()); 20 | for(const string& str:strings) 21 | { 22 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | float od1,od2; 9 | cin>>od1>>od2; 10 | float distance = (od2-od1); 11 | float remuneration = (round(distance*25)*10)/10; 12 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int n,arr[100]; 6 | cin>>n; 7 | for(int i=0;i>arr[i]; 10 | } 11 | int count=0; 12 | for(int i=0;i=n) 21 | count++; 22 | } 23 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Student { 7 | public: 8 | string name; 9 | int age; 10 | int marks; 11 | Student() : name(""), age(0), marks(0) {} 12 | Student(string n, int a, int m) : name(n), age(a), marks(m) {} 13 | }; 14 | 15 | int main() { 16 | int n; 17 | cin >> n; 18 | Student best_student; 19 | 20 | for (int i = 0; i < n; i++) { 21 | string name; 22 | int age, marks; 23 | cin >> name >> age >> marks; 24 | Student current_student(name, age, marks); 25 | 26 | if (current_student.marks > best_student.marks) { 27 | best_student = current_student; 28 | } 29 | } 30 | 31 | cout << best_student.name << " " << best_student.age << " " << best_student.marks << endl; 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /polymorphism-friend/constructor-overloading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Item { 5 | public: 6 | int val; 7 | 8 | 9 | Item (int val) { 10 | val = val; 11 | } 12 | 13 | Item operator>>(int val) { 14 | cin>>val; 15 | } 16 | }; 17 | 18 | int main() { 19 | string name; 20 | int i; 21 | float price; 22 | int quan; 23 | cin>>name; 24 | cin>>price; 25 | cin>>quan; 26 | 27 | cout<<"Item details:"< 2 | using namespace std; 3 | 4 | int fun1(int num1, int num2) { 5 | return num1 + num2; 6 | } 7 | 8 | int fun1(int num1, int num2, int num3) { 9 | return num1 * num2 * num3; 10 | } 11 | 12 | int main() { 13 | int n; 14 | cin >> n; 15 | 16 | if (n == 2) { 17 | int num1, num2; 18 | cin >> num1 >> num2; 19 | cout << fun1(num1, num2) << endl; 20 | } else if (n == 3) { 21 | int num1, num2, num3; 22 | cin >> num1 >> num2 >> num3; 23 | cout << fun1(num1, num2, num3) << endl; 24 | } else { 25 | cout << "Invalid Input" << endl; 26 | } 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /polymorphism-friend/new-oper-overloading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | int n; 7 | cin>>n; 8 | int arr[n]; 9 | int *a = new int; 10 | delete a; 11 | for(int i=0; i>arr[i]; 13 | } 14 | 15 | cout<<"New operator overloading"< 2 | #include 3 | using namespace std; 4 | 5 | class finalVelocity { 6 | private: 7 | float inVel, acc, tim; 8 | 9 | public: 10 | finalVelocity(float inVel, float acc, float tim){ 11 | inVel = inVel; 12 | acc = acc; 13 | tim = tim; 14 | } 15 | 16 | }; 17 | 18 | int main() { 19 | float inVel, acc, tim; 20 | cin>>inVel>>acc>>tim; 21 | 22 | float finalVel = inVel + (acc * tim); 23 | cout< 2 | #include 3 | using namespace std; 4 | 5 | float calculateTotalPrice(float price) { 6 | return price; 7 | } 8 | 9 | float calculateTotalPrice(float price, int quantity) { 10 | return price * quantity; 11 | } 12 | 13 | float calculateTotalPrice(float price, int quantity, float discountPercentage) { 14 | float discountedPrice = price * quantity * (1 - discountPercentage / 100); 15 | return discountedPrice; 16 | } 17 | 18 | int main() { 19 | int choice; 20 | while (true) { 21 | cin >> choice; 22 | if (choice == 4) { 23 | break; 24 | } 25 | 26 | float price; 27 | int quantity; 28 | float discountPercentage; 29 | 30 | if (choice == 1) { 31 | cin >> price; 32 | cout << fixed << setprecision(2) << calculateTotalPrice(price) << endl; 33 | } else if (choice == 2) { 34 | cin >> price >> quantity; 35 | cout << fixed << setprecision(2) << calculateTotalPrice(price, quantity) << endl; 36 | } else if (choice == 3) { 37 | cin >> price >> quantity >> discountPercentage; 38 | cout << fixed << setprecision(2) << calculateTotalPrice(price, quantity, discountPercentage) << endl; 39 | } else { 40 | cout << "Invalid choice" << endl; 41 | } 42 | } 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /templates-standard-library/class-template.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template 5 | class OddEvenPrinter { 6 | public: 7 | void printOdd(T arr[], int n) { 8 | for (int i = 1; i < n; i += 2) { 9 | cout << arr[i] << " "; 10 | } 11 | cout << endl; 12 | } 13 | }; 14 | 15 | int main() { 16 | int n; 17 | cin >> n; 18 | int arr[n]; 19 | string str[n]; 20 | for (int i = 0; i < n; i++) { 21 | cin >> arr[i]; 22 | } 23 | for (int i = 0; i < n; i++) { 24 | cin >> str[i]; 25 | } 26 | OddEvenPrinter intPrinter; 27 | intPrinter.printOdd(arr, n); 28 | OddEvenPrinter strPrinter; 29 | strPrinter.printOdd(str, n); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /templates-standard-library/currency-converter.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Currency { 8 | public: 9 | double amount; 10 | string code; 11 | 12 | Currency(double amt, const string& currencyCode) : amount(amt), code(currencyCode) {} 13 | }; 14 | 15 | class Converter { 16 | public: 17 | static double convertCurrency(double amount, const string& fromCurrencyCode, const string& toCurrencyCode) { 18 | if (fromCurrencyCode == "USD" && toCurrencyCode == "EUR") { 19 | return amount * 0.85; 20 | } else if (fromCurrencyCode == "EUR" && toCurrencyCode == "USD") { 21 | return amount * 1.18; 22 | } else { 23 | return -1.0; 24 | } 25 | } 26 | }; 27 | 28 | int main() { 29 | double amount; 30 | string fromCurrencyCode, toCurrencyCode; 31 | 32 | cin >> amount >> fromCurrencyCode >> toCurrencyCode; 33 | 34 | Currency sourceCurrency(amount, fromCurrencyCode); 35 | Currency targetCurrency(0, toCurrencyCode); 36 | 37 | double convertedAmount = Converter::convertCurrency(sourceCurrency.amount, sourceCurrency.code, targetCurrency.code); 38 | 39 | cout << fixed << setprecision(2); 40 | if (sourceCurrency.code == "USD") { 41 | cout << sourceCurrency.amount << " USD is equivalent to " << convertedAmount << " " << targetCurrency.code << endl; 42 | } else if (sourceCurrency.code == "EUR") { 43 | cout << sourceCurrency.amount << " EUR is equivalent to " << convertedAmount << " " << targetCurrency.code << endl; 44 | } 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /templates-standard-library/function-template.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template 5 | void concatenate(T arr1[], int n1, T arr2[], int n2) { 6 | cout <<"Concatenated array: "; 7 | for (int i = 0; i < n1; i++) { 8 | cout << arr1[i] + arr2[i] << " "; 9 | } 10 | cout << endl; 11 | } 12 | 13 | template 14 | 15 | void concatenateWithDelimiter(T s1, T s2, string delimiter) { 16 | cout<<"Concatenated string: "<< s1 << delimiter << s2 << endl; 17 | } 18 | 19 | int main() { 20 | int arr1[3], arr2[3]; 21 | string s1, s2, delimiter; 22 | for (int i = 0; i < 3; i++) { 23 | cin >> arr1[i]; 24 | } 25 | for (int i = 0; i < 3; i++) { 26 | cin >> arr2[i]; 27 | } 28 | cin >> s1 >> s2 >> delimiter; 29 | concatenate(arr1, 3, arr2, 3); 30 | concatenateWithDelimiter(s1, s2, delimiter); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /templates-standard-library/search-function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | // Input 7 | int n; 8 | std::cin >> n; 9 | 10 | std::vector elements(n); 11 | for (int i = 0; i < n; ++i) { 12 | std::cin >> elements[i]; 13 | } 14 | 15 | int target; 16 | std::cin >> target; 17 | 18 | // Use std::find to find the first occurrence of the target element 19 | auto it = std::find(elements.begin(), elements.end(), target); 20 | 21 | // Output 22 | if (it != elements.end()) { 23 | // Target element found, use std::find_if to find all occurrences 24 | std::cout << "Element " << target << " found at positions:"; 25 | for (; it != elements.end(); it = std::find_if(it + 1, elements.end(), [target](int val) { return val == target; })) { 26 | std::cout << " " << std::distance(elements.begin(), it) + 1; 27 | } 28 | std::cout << std::endl; 29 | } else { 30 | // Target element not found 31 | std::cout << "Element " << target << " not found in the vector." << std::endl; 32 | } 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /templates-standard-library/sort-stl.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | #include 3 | #include 4 | 5 | int main() { 6 | // Input 7 | int n; 8 | std::cin >> n; 9 | 10 | int sequence[10]; // Assuming a maximum size of 10 as per the code constraint 11 | for (int i = 0; i < n; ++i) { 12 | std::cin >> sequence[i]; 13 | } 14 | 15 | int k; 16 | std::cin >> k; 17 | 18 | // Sorting the sequence in descending order 19 | std::sort(sequence, sequence + n, std::greater()); 20 | 21 | // Output 22 | if (k >= 1 && k <= n) { 23 | std::cout << "The kth largest element is: " << sequence[k - 1] << std::endl; 24 | } else { 25 | std::cout << "Invalid value of k" << std::endl; 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /templates-standard-library/vector-stl.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nishant-Tiwari24/object-oriented-programming/85a217a151ce49632a4c93608a765c7a5ba6cd67/templates-standard-library/vector-stl.cpp -------------------------------------------------------------------------------- /types-of-inheritance/GPA-multiple-inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class GPA { 7 | protected: 8 | double gpa; 9 | 10 | public: 11 | GPA(double grade) : gpa(grade) {} 12 | }; 13 | 14 | class CreditHours { 15 | protected: 16 | int creditHours; 17 | 18 | public: 19 | CreditHours(int hours) : creditHours(hours) {} 20 | }; 21 | 22 | class Attendance { 23 | protected: 24 | double attendance; 25 | 26 | public: 27 | Attendance(double percent) : attendance(percent) {} 28 | }; 29 | 30 | class Student : public GPA, public CreditHours, public Attendance { 31 | public: 32 | Student(double grade, int hours, double percent) 33 | : GPA(grade), CreditHours(hours), Attendance(percent) {} 34 | 35 | double calculateGradePoints() { 36 | double gradePoints = gpa * creditHours; 37 | if (attendance > 80.0) { 38 | gradePoints += 5.0; 39 | } 40 | return gradePoints; 41 | } 42 | }; 43 | 44 | int main() { 45 | double gpa, attendance; 46 | int creditHours; 47 | cin >> gpa >> creditHours >> attendance; 48 | Student patrick(gpa, creditHours, attendance); 49 | cout << fixed << setprecision(1) << patrick.calculateGradePoints() << endl; 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /types-of-inheritance/Parent-inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Parent { 4 | public: 5 | void fun(int num) { 6 | int digits[10]; 7 | int count = 0; 8 | int sum = 0; 9 | while (num > 0) { 10 | digits[count++] = num % 10; 11 | num /= 10; 12 | } 13 | for (int i = 0; i < count; ++i) { 14 | for (int j = i + 1; j < count; ++j) { 15 | sum += digits[i] + digits[j]; 16 | } 17 | } 18 | 19 | // Displaying the final sum 20 | std::cout << sum << std::endl; 21 | } 22 | }; 23 | 24 | class Child : public Parent { 25 | public: 26 | void parentFunction(int num) { 27 | // Calling the fun method from the Parent class 28 | fun(num); 29 | } 30 | }; 31 | 32 | int main() { 33 | int num; 34 | std::cin >> num; 35 | Child child; 36 | 37 | child.parentFunction(num); 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /types-of-inheritance/appetizer-menu-maincourse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class MenuItem { 6 | protected: 7 | double price; 8 | 9 | public: 10 | MenuItem(double p) : price(p) {} 11 | 12 | virtual double calculatePrice(int persons) = 0; 13 | }; 14 | 15 | class Appetizer : public MenuItem { 16 | public: 17 | Appetizer(double p) : MenuItem(p) {} 18 | 19 | double calculatePrice(int persons) override { 20 | return price * persons; 21 | } 22 | }; 23 | 24 | class MainCourse : public MenuItem { 25 | public: 26 | MainCourse(double p) : MenuItem(p) {} 27 | 28 | double calculatePrice(int persons) override { 29 | return price * persons; 30 | } 31 | }; 32 | 33 | int main() { 34 | int numPersons, numAppetizers, numMainCourses; 35 | std::cin >> numPersons >> numAppetizers >> numMainCourses; 36 | 37 | std::vector appetizerPrices(numAppetizers); 38 | std::vector mainCoursePrices(numMainCourses); 39 | 40 | for (int i = 0; i < numAppetizers; ++i) { 41 | std::cin >> appetizerPrices[i]; 42 | } 43 | 44 | for (int i = 0; i < numMainCourses; ++i) { 45 | std::cin >> mainCoursePrices[i]; 46 | } 47 | 48 | double totalCost = 0; 49 | 50 | for (int i = 0; i < numAppetizers; ++i) { 51 | Appetizer appetizer(appetizerPrices[i]); 52 | totalCost += appetizer.calculatePrice(numPersons); 53 | } 54 | 55 | for (int i = 0; i < numMainCourses; ++i) { 56 | MainCourse mainCourse(mainCoursePrices[i]); 57 | totalCost += mainCourse.calculatePrice(numPersons); 58 | } 59 | 60 | std::cout << "Rs. " << std::fixed << std::setprecision(2) << totalCost << std::endl; 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /types-of-inheritance/cart-kitchen-store.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Cart { 7 | protected: 8 | double price; 9 | int quantity; 10 | public: 11 | Cart(double p, int q) : price(p), quantity(q) {} 12 | virtual double calculateTotal() { 13 | return price * quantity; 14 | } 15 | }; 16 | 17 | class Kitchen : public Cart { 18 | private: 19 | double discount; 20 | public: 21 | Kitchen(double p, int q, double d) : Cart(p, q), discount(d) {} 22 | double calculateTotal() override { 23 | double totalPrice = price * quantity; 24 | totalPrice -= totalPrice * (discount / 100); 25 | return totalPrice; 26 | } 27 | }; 28 | 29 | class Electronics : public Cart { 30 | private: 31 | double serviceFees; 32 | public: 33 | Electronics(double p, int q, double sf) : Cart(p, q), serviceFees(sf) {} 34 | double calculateTotal() override { 35 | return price * quantity + serviceFees; 36 | } 37 | }; 38 | 39 | int main() { 40 | double price; 41 | int quantity, category; 42 | double discountOrServiceFees; 43 | 44 | cin >> price >> quantity; 45 | cin >> category; 46 | cin >> discountOrServiceFees; 47 | 48 | Cart* item; 49 | if (category == 1) { 50 | item = new Kitchen(price, quantity, discountOrServiceFees); 51 | } else if (category == 2) { 52 | item = new Electronics(price, quantity, discountOrServiceFees); 53 | } else { 54 | cout << "Invalid category!"; 55 | return 0; 56 | } 57 | 58 | double totalPrice = item->calculateTotal(); 59 | cout << fixed << setprecision(2); 60 | cout << totalPrice << endl; 61 | 62 | delete item; 63 | 64 | return 0; 65 | } -------------------------------------------------------------------------------- /types-of-inheritance/fruit-inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Fruit { 5 | public: 6 | virtual std::string getTaste() { 7 | return "Generic fruit taste"; 8 | } 9 | }; 10 | 11 | class Apple : public Fruit { 12 | public: 13 | std::string getTaste() override { 14 | return "Sweet"; 15 | } 16 | }; 17 | 18 | class Banana : public Fruit { 19 | public: 20 | std::string getTaste() override { 21 | return "Creamy"; 22 | } 23 | }; 24 | 25 | class Orange : public Fruit { 26 | public: 27 | std::string getTaste() override { 28 | return "Citrusy"; 29 | } 30 | }; 31 | 32 | class Grape : public Fruit { 33 | public: 34 | std::string getTaste() override { 35 | return "Juicy"; 36 | } 37 | }; 38 | 39 | class Pineapple : public Fruit { 40 | public: 41 | std::string getTaste() override { 42 | return "Tangy"; 43 | } 44 | }; 45 | 46 | int main() { 47 | int choice; 48 | 49 | // Input 50 | std::cin >> choice; 51 | 52 | // Create an instance of Fruit pointer 53 | Fruit* fruit = nullptr; 54 | 55 | // Based on the choice, assign the appropriate derived class 56 | switch (choice) { 57 | case 1: 58 | fruit = new Apple(); 59 | break; 60 | case 2: 61 | fruit = new Banana(); 62 | break; 63 | case 3: 64 | fruit = new Orange(); 65 | break; 66 | case 4: 67 | fruit = new Grape(); 68 | break; 69 | case 5: 70 | fruit = new Pineapple(); 71 | break; 72 | default: 73 | std::cout << "Invalid choice, defaulting to generic fruit." << std::endl; 74 | fruit = new Fruit(); 75 | break; 76 | } 77 | 78 | // Display result 79 | std::cout << "The taste of the fruit is: " << fruit->getTaste() << std::endl; 80 | 81 | // Release memory 82 | delete fruit; 83 | 84 | return 0; 85 | } -------------------------------------------------------------------------------- /types-of-inheritance/hirearichal-inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Circle { 6 | protected: 7 | double radius; 8 | 9 | public: 10 | Circle(double r) : radius(r) {} 11 | virtual double calculateCircumference() = 0; 12 | }; 13 | 14 | class Class1 : public Circle { 15 | public: 16 | Class1(double r) : Circle(r) {} 17 | double calculateCircumference() override { 18 | return 2 * 3.14159 * radius; 19 | } 20 | }; 21 | 22 | class Class2 : public Circle { 23 | public: 24 | Class2(double r) : Circle(r) {} 25 | double calculateCircumference() override { 26 | return 23.14 * radius; 27 | } 28 | }; 29 | 30 | int main() { 31 | double radius; 32 | cin >> radius; 33 | Class1 obj1(radius); 34 | Class2 obj2(radius); 35 | 36 | cout << "Class 1: " << fixed << setprecision(2) << obj1.calculateCircumference() << endl; 37 | cout << "Class 2: " << fixed << setprecision(2) << obj2.calculateCircumference() << endl; 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /types-of-inheritance/hybrid-inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Product { 8 | protected: 9 | string name; 10 | double price; 11 | int quantity; 12 | 13 | public: 14 | Product(const string& name, double price, int quantity) 15 | : name(name), price(price), quantity(quantity) {} 16 | 17 | double calculateCost() { 18 | return price * quantity; 19 | } 20 | }; 21 | 22 | class Electronics : public Product { 23 | public: 24 | Electronics(const string& name, double price, int quantity) 25 | : Product(name, price, quantity) {} 26 | 27 | double calculateElectronicsCost() { 28 | if (quantity >= 3) { 29 | return calculateCost() * 0.9; 30 | } else { 31 | return calculateCost(); 32 | } 33 | } 34 | }; 35 | 36 | class Clothing : public Product { 37 | public: 38 | Clothing(const string& name, double price, int quantity) 39 | : Product(name, price, quantity) {} 40 | 41 | double calculateClothCost() { 42 | if (quantity >= 5) { 43 | return calculateCost() * 0.95; 44 | } else { 45 | return calculateCost(); 46 | } 47 | } 48 | }; 49 | 50 | class OrderCalculator : public Electronics, public Clothing { 51 | public: 52 | OrderCalculator(const string& electronicName, double electronicPrice, int electronicQuantity, 53 | const string& clothName, double clothPrice, int clothQuantity) 54 | : Electronics(electronicName, electronicPrice, electronicQuantity), 55 | Clothing(clothName, clothPrice, clothQuantity) {} 56 | 57 | double calculateTotalCost() { 58 | double totalCost = calculateElectronicsCost() + calculateClothCost(); 59 | return totalCost; 60 | } 61 | }; 62 | 63 | int main() { 64 | string electronicName, clothName; 65 | double electronicPrice, clothPrice; 66 | int electronicQuantity, clothQuantity; 67 | 68 | // Input 69 | cin >> electronicName >> electronicPrice >> electronicQuantity; 70 | cin >> clothName >> clothPrice >> clothQuantity; 71 | 72 | // Calculate total order cost 73 | OrderCalculator order(electronicName, electronicPrice, electronicQuantity, clothName, clothPrice, clothQuantity); 74 | double totalCost = order.calculateTotalCost(); 75 | 76 | // Output 77 | cout << "Total Order Cost: $" << std::fixed << std::setprecision(2) << totalCost << std::endl; 78 | 79 | return 0; 80 | } -------------------------------------------------------------------------------- /types-of-inheritance/multilevel-inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Shape { 5 | protected: 6 | int l; 7 | int b; 8 | 9 | public: 10 | Shape(int a, int b) : l(a) , b(b){} 11 | 12 | int area() { 13 | return l*b; 14 | } 15 | 16 | int perimeter() { 17 | return 2*(l+b); 18 | } 19 | }; 20 | 21 | class Rectangle : public Shape { 22 | protected: 23 | int l; 24 | int b; 25 | 26 | public: 27 | Rectangle(int l, int b) : Shape(l,b){} 28 | void recArea() { 29 | cout<<"Rectangle Area: "<>l>>b>>s; 49 | Rectangle r(l,b); 50 | Square s1(s); 51 | r.recArea(); 52 | s1.sqArea(); 53 | return 0; 54 | } -------------------------------------------------------------------------------- /types-of-inheritance/multiple-inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Triangle { 6 | public: 7 | double a; 8 | double b; 9 | 10 | Triangle(double a1,double b1): a(a1) , b(b1){} 11 | 12 | double calculateArea() { 13 | return 0.5*a*b; 14 | } 15 | }; 16 | 17 | class Hypotenuse : public Triangle { 18 | public: 19 | Hypotenuse(double a2, double b2): Triangle(a2 , b2){} 20 | 21 | double Hypo() { 22 | return sqrt(pow(a,2) + pow(b,2)); 23 | } 24 | 25 | double Perimeter() { 26 | return a + b + Hypo(); 27 | } 28 | }; --------------------------------------------------------------------------------