2016-04-07 20 views
-3

Bu hatayı almamak için bu kodun neyi yanlış?cout/cin türünde bir hata bildirmiyor

Hata, yalnızca "distanceFormula" yı ana yerine koymak yerine, kendi sınıfını yaptım.

#include <iostream> 
#include <string> 

using namespace std; 

class distanceFormula { 
    public: 
int speed; 
int time; 
int distance; 

cout << "What is the speed?" << endl; 
cin >> speed; 

cout << "How long did the action last?" << endl; 
cin >> time; 


distance = speed * time; 

cout << "The distance traveled was " << distance << endl; 
}; 




int main() 
{ 
distanceFormula ao 
ao.distanceFormula; 

return 0; 
}; 
+0

Neden kendi sınıfını yaptınız? Kodunuzu derlediniz mi? –

+1

Ayırıcı işlev ve sınıf. –

+1

'ao.distanceFormula;' Bu ne yapmalı? – drescherjm

cevap

0

veri veya fonksiyonu bildirimleri ve isteğe bağlı olarak erişim belirteçleri olabilir ya sadece üye içerebilir sınıf bildirimi, bir gövde.

bir işlev içinde kodunuzu sarın ve hala bir sınıf kullanmak zorunda o zaman bir nesne

class distanceFormula { 
public: 
int speed; 
int time; 
int distance; 
void init() 
{ 
    cout << "What is the speed?" << endl; 
    cin >> speed; 

    cout << "How long did the action last?" << endl; 
    cin >> time; 
    distance = speed * time; 
    cout << "The distance traveled was " << distance << endl; 
} 
}; 

int main() 
{ 
    distanceFormula ao; 
    ao.init(); 
    return 0; 
}; 
+0

Teşekkürler! eleştirmeyen tek XD –

+0

@AmmarT. Cevabı güncelledim, sebebini de ekledim! –

0

tarafından main bu resimleri. İşte nasılsınız:

#include <iostream> 

class distanceFormula 
{ 
private: 
    int speed; // private 
    int time; // private 
public: 
    distanceFormula(); // constructor 
    int getSpeed(); // to get 
    int getTime(); // to get 
    int getDistance(); // to get 
    void setSpeed(int); // to set 
    void setTime(int); // to set 
}; 

distanceFormula::distanceFormula() 
{ 
    this->time = 0; 
    this->speed = 0; 
} 

int distanceFormula::getSpeed() 
{ 
    return this->speed; 
} 
int distanceFormula::getTime() 
{ 
    return this->time; 
} 
int distanceFormula::getDistance() 
{ 
    return this->time * this->speed; 
} 
void distanceFormula::setSpeed(int speedVal) 
{ 
    this->speed = speedVal; 
} 
void distanceFormula::setTime(int timeVal) 
{ 
    this->time = timeVal; 
} 


int main() 
{ 
    distanceFormula YourObject; // create obj 
    int SpeedValue; 
    int TimeValue; 
    std::cout << "Enter the Speed:"; 
    std::cin >> SpeedValue; // take speed 

    std::cout << "Enter the Time:"; 
    std::cin >> TimeValue; // take time 


    YourObject.setSpeed(SpeedValue); // set 
    YourObject.setTime(TimeValue); // set 

    std::cout << "This is the distance: " << YourObject.getDistance(); // retrieve result 

    getchar(); // wait 
    return 0; 
} 
İlgili konular