Olio-ohjelmointi
Lisätehtäviä

Tässä on joukko ChatGPT:n luomia harjoitustehtäviä.


Exercise 1: Creating a Class

Create a class Car with the following private attributes: string make, string model, and int year. Write a constructor to initialize these attributes and create a method void display() that prints the car's details.

Exercise 2: Encapsulation

Modify the Car class from Exercise 1 by adding getter and setter methods for each attribute. Ensure that only valid data can be set (e.g., the year should be greater than 1885).

Exercise 3: Constructor Overloading

In the Car class, add an overloaded constructor that only takes make and model as parameters. In this case, set the default value of year to 2020. Use this overloaded constructor to create objects.

Exercise 4: Inheritance

Create a class ElectricCar that inherits from the Car class. Add a private attribute int batteryCapacity and a method void charge() to display a message that the car is charging.

Exercise 5: Polymorphism

In the ElectricCar class, override the display() method to include information about the battery capacity. Create a vector of Car objects that contains both Car and ElectricCar objects. Use polymorphism to call the display() method for each object.

OHJE: tässä tehtävässä kannattaa vektoriin lisätä osoittimia, eikä olioita. Se voidaan tehdä seuraavasti:

vector<unique_ptr<Car>> carList;

carList.push_back(make_unique<Car>());
carList.push_back(make_unique<ElectricCar>());

for (const auto& obj : carList) {
        obj->display(); 
}
    
Jos vektoriin tallennetaan olioita, ei voida kutsua perivän luokan metodeja. for-loopissa käytetty sana auto antaa kääntäjän päätellä muuttujan tyypin sen alustuksen perusteella.

Exercise 6: Virtual Functions

Make the display() method in the Car class a virtual function, and demonstrate how virtual functions enable runtime polymorphism by using a base class pointer to point to a derived class object and call the display() method.

Lause "using a base class pointer to point to a derived class object" tarkoittaa siis tällaista

    Car *objectCar;
    ElectricCar objectElectricCar;
    objectCar=&objectElecticCar;
    

Exercise 7: Abstract Class

Create an abstract class Vehicle with a pure virtual function void start(). Inherit from this class to create the Car and Motorcycle classes. Implement the start() method for both derived classes.

Exercise 8: Multiple Inheritance

Create a class HybridCar that inherits from both the Car class and another class FuelCar. Add an attribute int fuelTankCapacity to the FuelCar class and demonstrate how multiple inheritance works in C++.

Exercise 9: Destructor

Implement a destructor in the Car class that displays a message when a Car object is destroyed. Create and destroy objects manually in the main function to observe how destructors are called.

Exercise 10: Static Members

Add a static data member int carCount in the Car class to track the number of car objects created. Implement a static member function getCarCount() to return this value. Test the static members by creating multiple car objects.

h2>Exercise 3: Composition with Dynamic Memory

Create two classes: Book and Library. The Book class should have:

  • string attributes: title and author.
  • A constructor to initialize these attributes.
  • A method display() that prints the book's details.

The Library class should:

  • Contain a pointer to a dynamically allocated array of Book objects.
  • Have an integer attribute bookCount to track the number of books.
  • A constructor that initializes the dynamic array and the bookCount.
  • A method addBook() to add a new Book to the array.
  • A method displayBooks() to print all books in the library.
  • A destructor to release the allocated memory.

Task: Implement both classes and test them by creating a Library object, adding books, and displaying them.

Exercise 11: Composition

Create three classes: Screen, Keyboard, and Laptop.

Screen should have:

  • A float attribute size (in inches).
  • A method display() to print the screen size.

Keyboard should have:

  • A string attribute layout (e.g., "QWERTY").
  • A method display() to print the keyboard layout.

Laptop should have:

  • A Screen object as a member.
  • A Keyboard object as a member.
  • A method display() to print both the screen and keyboard details.

Task: Implement the classes and test them by creating a Laptop object and calling its display() method.

Exercise 12: Composition (Nested Composition)

Create three classes: Address, Person, and Company.

Address should have:

  • string attributes: street and city.
  • A constructor to initialize the attributes.
  • A method display() to print the address.

Person should have:

  • A string attribute name.
  • An Address object as a member.
  • A constructor to initialize both name and Address.
  • A method display() to print the person's name and address.

Company should have:

  • A string attribute companyName.
  • A Person object as a member (e.g., the CEO).
  • A constructor to initialize both companyName and the Person.
  • A method display() to print the company's name and CEO details.

Task: Implement the classes and test them by creating a Company object and calling its display() method.

Termi "Nested" viittaa siihen, että

  • Company sisältää Person luokan olion
  • Person sisältää Address luokan olion

Exercise 13: Exception Handling

In the Car class, create a method void drive(int distance) that throws an exception if the distance is negative. Use try and catch blocks to handle this exception in the main function.

Exercise 14: Friend Functions

Huom! Friend funktio ei kuulu kurssin sisältöön. Tässä lyhyesti siitä:

  • friend-funktio on erikoisfunktio, joka saa pääsyn luokan yksityisiin (private) ja suojattuihin (protected) jäseniin, vaikka se ei ole kyseisen luokan jäsenfunktio.
  • Friend-funktio määritetään friend-avainsanalla luokan sisällä, mutta sitä ei varsinaisesti määritellä luokan jäsenfunktioksi.
  • Friend-funktio toteutetaan normaalina funktiona luokan ulkopuolella.
Tuolta löytyy esimerkki https://www.tutorialspoint.com/cplusplus/cpp_friend_functions.htm

Write a friend function compareCars() that compares the year attribute of two Car objects and returns the newer car. Demonstrate the use of this friend function in the main function.



Toggle Menu