के लिए प्रिंट फ़ंक्शन मैं कक्षा ऑटोटाटा के लिए एक प्रिंट फ़ंक्शन लिखना चाहता हूं जिसमें कारों के बारे में जानकारी है। इस प्रिंट फ़ंक्शन के साथ, मैं आदर्श रूप से एक वेक्टर मुद्रित करना चाहता हूं जिसमें कई अलग-अलग वर्ग वस्तुएं हों। मैं पहले से ही वस्तुओं के प्रत्येक तत्व के लिए प्राप्त कार्यों में लिखा है, लेकिन मैं अभी भी कैसे उन का उपयोग कर के बारे में जाने के लिए निम्न स्वरूप में डेटा बाहर मुद्रित करने के लिए एक समारोह लिखने के लिए का एक सा अनिश्चित हूं:कक्षा सी ++
mpg:cylinders:displacement:horsepower:weight:acceleration:modelYear:origin:carName
उदाहरण के लिए :
10.0:8:360.0:215.0:4615.:14.0:70:1:ford f250
10.0:8:307.0:200.0:4376.:15.0:70:1:chevy c20
11.0:8:318.0:210.0:4382.:13.5:70:1:dodge d200
वर्ग है:
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class AutoData {
public:
AutoData()
{
mpg = 0;
cylinders = 0;
displacement = 0;
horsepower = 0;
weight = 0;
acceleration = 0;
modelYear = 0;
origin = 0;
carName = "";
}
AutoData(const AutoData & rhs)
{
setAuto(rhs.mpg, rhs.cylinders, rhs.displacement, rhs.horsepower, rhs.weight, rhs.acceleration, rhs.modelYear, rhs.origin, rhs.carName);
}
void setAuto(float mp, int cy, float di, float ho, float we, float ac, int mo, int o, string ca)
{
mpg = mp;
cylinders = cy;
displacement = di;
horsepower = ho;
weight = we;
acceleration = ac;
modelYear = mo;
origin = o;
carName = ca;
}
const float & getmpg() const
{
return mpg;
}
const int & getcylinders() const
{
return cylinders;
}
const float & getdisplacement() const
{
return displacement;
}
const float & gethorsepower() const
{
return horsepower;
}
const float & getweight() const
{
return weight;
}
const float & getacceleration() const
{
return acceleration;
}
const int & getmodelYear() const
{
return modelYear;
}
const int & getorigin() const
{
return origin;
}
const string & getcarName() const
{
return carName;
}
bool operator == (const AutoData & rhs) const
{
if(getmpg() == rhs.getmpg())
{
return gethorsepower() == rhs.gethorsepower();
}
else
{
return false;
}
}
bool operator > (const AutoData & rhs) const
{
if(rhs.getmpg() > getmpg())
{
return true;
}
else if(getmpg() == rhs.getmpg())
{
if(rhs.gethorsepower() > gethorsepower())
{
return true;
}
}
else
{
return false;
}
}
private:
float mpg;
int cylinders;
float displacement;
float horsepower;
float weight;
float acceleration;
int modelYear;
int origin;
string carName;
};
कोई मदद/सलाह किसी को भी प्रदान कर सकते हैं बहुत सराहना की जाएगी !! धन्यवाद
अगर आप सिर्फ अदालत सांत्वना देने << एमपीजी << प्रिंट करना चाहते हैं ":" << सिलेंडरों << "" यदि आप स्ट्रिंग में स्टोर करना चाहते हैं तो स्ट्रिंगस्ट्रीम का उपयोग करें। – Mike
लेकिन मैं इनके पूरे वेक्टर को कैसे मुद्रित करूं? ताकि mpg1: cylinders1: आदि ... mpg2: cylinders2: आदि ... –
बस वेक्टर पर फिर से सक्रिय करें और प्रविष्टियों के बीच नई लाइनें जोड़ें – stefaanv