2. Sorunun cevabı belki işinize yarar
#include <iostream>
#include <string>
using namespace std;
using std::cout;
using std::endl;
class Person{
public:
Person(){};
Person(string str){name = str;}
string getName() const {return name;}
private:
string name;
};
//a şıkkı
class Car{
public:
Car(string ad,int a,int b){name = ad;HP=a;weight = b;}
string getName(){return name.getName();}
int getHP() const{return HP;}
int getWeight() const {return weight;}
float calculateTPRfunction() const {return static_cast<float>(HP)/static_cast<float>(weight);}
protected:
Person name;
int HP;
int weight;
};
//b i)
void whoIsFaster(Car a,Car b){
if(a.calculateTPRfunction()>b.calculateTPRfunction()){
cout<<a.getName()<<"'s TPR is bigger."<<endl;
}
else if(b.calculateTPRfunction()>a.calculateTPRfunction()){
cout<<b.getName()<<"'s TPR is bigger."<<endl;
}
else {
cout<<a.getName()<<" and "<<b.getName()<<"'s TPRs are equal."<<endl;
}
}
//b ii) Hayır friend olarak tanımlaya gerek yok yukarıda da gödüldüğü gibi :)
//çünkü her car nesnesinin kendi get fonksiyonları var.
//c şıkkı
class Truck:public Car{
public:
Truck(string ad,int b,int c,int a):Car(ad,b,c){loadcapacity = a;}
int getLoadCapacity() const {return loadcapacity;}
float calculateTPRfunction() const {return static_cast<float>(getHP())/(static_cast<float>(loadcapacity)+static_cast<float>(getWeight()));}
private:
int loadcapacity;
};
//d şıkkı
int main(){
Car car1("Elif",90,1100);
Car car2("Ali",112,1260);
cout<<"Owner of first car is "<<car1.getName()<<" HP is "<<car1.getHP()<<" weight is "<<car1.getWeight()<<endl;
cout<<"Owner of second car is "<<car2.getName()<<" HP is "<<car2.getHP()<<" weight is "<<car2.getWeight()<<endl;
cout<<endl<<"TPR of "<<car1.getName()<<"'s car is "<<car1.calculateTPRfunction()<<endl;
cout<<"TPR of "<<car2.getName()<<"'s car is "<<car2.calculateTPRfunction()<<endl<<endl;
whoIsFaster(car1,car2);
Truck truck("Can",320,2600,2000);
cout<<endl<<"Owner of truck is "<<truck.getName()<<" HP is "<<truck.getHP()<<" weight is "
<<truck.getWeight()<<" load capacity is "<<truck.getLoadCapacity()<<endl;
cout<<"TPR of "<<truck.getName()<<"'s truck is "<<truck.calculateTPRfunction()<<endl<<endl;
//e şıkkı
//truck nesneleri için whoisfasteri çağırabiliyoruz. Ama fonksiyonun içinde bunlar car nesnelerine dönüştürüldüğü için
//ilk calculateTPRfunction(override öncesi) ile hesaplanıyor ve yanlış sonuç veriyor.çünkü loadcapacity'i artık önemsemiyor
//aşağıdaki yorum satırlarını silerek deneyebilirsiniz.
// Truck truck1("Sedat",400,2000,600);
// whoIsFaster(truck,truck1);
getchar();
return 0;
}
|