Object Oriented Programming Exercises in C++ | Questions and Solutions
Example object-oriented programming questions and solutions in C++. If you found this video helpful, you can buy me a coffee: ☕ https://www.buymeacoffee.com/muratoksuzer 1. Simple Class and Object Create a class called Person with members name (string) and age (integer). Instantiate an object of the class, set values for name and age, and display them. 2. Constructor and Destructor Enhance the Person class from the previous question by adding a constructor and a destructor. Use the constructor to initialize values and the destructor to display a goodbye message. 3. Class Methods Extend the Person class to include a method introduce that prints a greeting including the person's name. Call this method for an object. 4. Inheritance Create a base class Shape with a virtual method calculateArea. Derive two classes, Circle and Rectangle, from Shape. Implement the calculateArea method for each derived class. Instantiate objects of both derived classes and display their areas. 5. Encapsulation Create a class Player with private members gold (long) and accountNumber (string). Implement public methods earn and spend to modify the gold balance. Use encapsulation to protect the private members. 6. Operator Overloading Overload the + operator for the Player class to allow adding the golds of two accounts. Instantiate two Player objects, add their golds, and display the result. 7. Static Members Create a class Counter with a static member count. Implement methods increment and getCount to modify and retrieve the count. Instantiate objects of the class and observe the behavior of the static member. 8. Friend Functions Create a class Distance to represent distances in feet and inches. Implement a friend function addDistances that adds two Distance objects. Instantiate objects, add their distances, and display the result. 9. Polymorphism Create a base class Animal with a virtual method makeSound. Derive two classes, Dog and Cat, from Animal. Implement the makeSound method for each derived class. Instantiate objects of both derived classes, call their makeSound methods, and display the sounds. 10. Virtual Destructors Create a base class Shape with a virtual destructor. Derive two classes, Circle and Rectangle, from Shape. Implement destructors that display goodbye messages. Instantiate objects of both derived classes, observe the behavior of virtual destructors, and display the messages. Learn Cpp by example. https://x.com/murat_dev01
Download
0 formatsNo download links available.