Ich habs mir mal als Frühmorgensaufgabe gesetzt:
CIce.cpp:
#include "stdafx.h"
#include "Ice.h"
using namespace std;
void printGreeting();
const char* getUserRequest();
const char* getFriendRequest();
const CIce& chooseIce(CIce& ice1, CIce& ice2, CIce& ice3);
int main(int argc, char* argv[])
{
CIce capri("Capri", 1.5);
CIce nogger("Nogger", 1.8);
CIce cornetto("Cornetto", 2.5);
double budget = 10.0;
printGreeting();
cout << getUserRequest() << endl;
CIce choosenIce = chooseIce(capri, nogger, cornetto);
budget -= choosenIce.getPrice();
cout << getFriendRequest() << endl;
choosenIce = chooseIce(capri, nogger, cornetto);
budget -= choosenIce.getPrice();
cout << "Restgeld:\t" << budget;
cin.get();
return 0;
}
void printGreeting()
{
cout << "Tach." << endl;
}
const char* getUserRequest()
{
return "Was willste?";
}
const char* getFriendRequest()
{
return "Was will dein Freund?";
}
const CIce& chooseIce(CIce& ice1, CIce& ice2, CIce& ice3)
{
cout << "1 = " << ice1.getName() << endl;
cout << "2 = " << ice2.getName() << endl;
cout << "3 = " << ice3.getName() << endl;
int buffer = 0;
cin >> buffer;
switch(buffer)
{
case 1:
return ice1;
case 2:
return ice2;
case 3:
return ice3;
}
}
Ice.h
#pragma once
#include "stdafx.h"
class CIce
{
private:
std::string m_name;
double m_price;
public:
CIce(std::string name, double price);
virtual ~CIce(void);
const std::string& getName();
double getPrice();
};
Ice.cpp:
#include "StdAfx.h"
#include "Ice.h"
CIce::CIce(std::string name, double price)
{
this -> m_name = name;
this -> m_price = price;
}
CIce::~CIce(void)
{
}
const std::string& CIce::getName()
{
return m_name;
}
double CIce::getPrice()
{
return m_price;
}
stdafx.h:
#pragma once
#include <iostream>
#include <string>
Kannst dir ja ein, zwei Sachen abgucken.
Sachen wie Benutzerfehleingaben usw. hab ich erstmal nicht berücksichtigt... :floet: