Tässä on joukko ChatGPT:n luomia harjoitustehtäviä.
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.
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).
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.
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.
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.
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;
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.
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++.
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.
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.
Create two classes: Book and Library. The Book
class should have:
string
attributes: title
and author
.display()
that prints the book's details.
The Library
class should:
Book
objects.bookCount
to track the number of books.bookCount
.addBook()
to add a new Book
to the array.displayBooks()
to print all books in the library.
Task: Implement both classes and test them by creating a Library
object, adding books, and displaying them.
Create three classes: Screen, Keyboard, and Laptop.
Screen should have:
float
attribute size
(in inches).display()
to print the screen size.Keyboard should have:
string
attribute layout
(e.g., "QWERTY").display()
to print the keyboard layout.Laptop should have:
Screen
object as a member.Keyboard
object as a member.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.
Create three classes: Address, Person, and Company.
Address should have:
string
attributes: street
and city
.display()
to print the address.Person should have:
string
attribute name
.Address
object as a member.name
and Address
.display()
to print the person's name and address.Company should have:
string
attribute companyName
.Person
object as a member (e.g., the CEO).companyName
and the Person
.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ä
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.
Huom! Friend funktio ei kuulu kurssin sisältöön. Tässä lyhyesti siitä:
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.