създаване на клас със функции - пример

Програмиране и програмни езици Решени задачи

// CarRateApp2.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <string>

class Car
{
public:
std::string brand;
std::string color;
int engineVol;
double price;

Car(std::string brandIn, std::string colorIn, double priceIn, int engineVolIn) {
brand = brandIn;
color = colorIn;
price = priceIn;
engineVol = engineVolIn;

};

bool WriteCar() {
std::cout << "Hello. The car stats are as follows:\n";
std::cout << "\nModel: " << brand;
std::cout << "\nColor: " << color;
std::cout << "\nEngine volume: " << engineVol <<" liters";
std::cout << "\nprice: $" << price;
return true;
};

double PriceDiff(double budget) {
double res = price - budget;
if (res > 0){
std::cout << "\nYou don't have enough money. You need " << res << " more to buy this car";
}
else
{
std::cout << "\nYou can buy the car. You will have $" << res << " left.";
}

return res;
};

void CheckEngine(Car otherCar) {
if (engineVol > otherCar.engineVol) {
std::cout << "\nThe car " << brand << " has bigger engine volume";
}
else if (engineVol < otherCar.engineVol)
{
std::cout << "\nThe car " << otherCar.brand << " has bigger engine volume";
}
else
{
std::cout << "\nBoth cars have the same brand!";
}
};
};

int main()
{
std::string carModel;
char carModelChar[200];
std::string carColor;
int carEngineVol;
double carPrice;
std::cout << "What is the model of the car?: \n";
std::cin.getline(carModelChar,200);
carModel = carModelChar;
std::cout << "What is the color?: \n";
std::cin >> carColor;
std::cout << "What is the price?: \n";
std::cin >> carPrice;

std::cout << carPrice << "\nWhat is the engine volume of the car?: \n";
std::cin >> carEngineVol;
Car homeCar(carModel,carColor,carPrice,carEngineVol);
Car friendCar("Opel Vectra", carColor, carPrice, carEngineVol+100);
homeCar.WriteCar();
homeCar.PriceDiff(200.0);
homeCar.CheckEngine(friendCar);
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Резултат:

Преглед на началото - целият файл след изтегляне

Описание

Задачата е доста проста. Трябва да се създаде клас Кола, който да има атрибути: марка, цвят, цена и обем на двигателя. Също, трябва да има и функция, която да сравнява обема на двигателя на колата, от която се извиква тя, и друга кола. И последно, трябва да има метод, който да проверява, дали потребителя може да купи колата или не. Живи и здрави и да живее 13-ката. Дисциплина: Софтуерно Инжинерство (C++)

0 коментара

Все още няма коментари. Бъдете първият, който ще коментира.

За да коментирате, трябва да сте влезли в профила си.

Влезте