Ir ao conteúdo
  • Cadastre-se

PC Trindade

Membro Júnior
  • Posts

    7
  • Cadastrado em

  • Última visita

Reputação

2
  1. #include <iostream> #include <vector> using namespace std; class Car { friend std::ostream &operator<<( std::ostream &, const Car & ); friend std::istream &operator>>( std::istream &, Car & ); public: Car(); //Custom default constructor Car(double Power2, double price2); //Custom constructor Car(const Car & obj); //Copy constructor ~Car(); //Custom destructor double getPower (); double getPrice (); void showinfo() const; static unsigned int getNumCars(); Car &setPower (double pwr); Car &setPrice (double pric); Car operator+(const Car &obj); //+ operator overload bool operator< (Car &obj); //< operator overload private: double Power; double price; static unsigned int number_of_cars; // number of mandatory airbags }; Car operator+(Car const& Power, Car const& Power2); Car operator-(Car const& Price, Car const& Price2); Car::Car (){ //Definition of initial values of the constructor Power=1000; price=500; std::cout<< "Created new car"<< std::endl; number_of_cars++; //increase the number of cars } Car::Car (double Power2, double price2){ Power=Power2; price=price2; std::cout<< "Created new car"<< std::endl; number_of_cars++; //increase the number of cars } //Copy constructor Car::Car(const Car & obj){ Power=obj.Power; price=obj.price; std::cout<< "copied Car"<< std::endl; number_of_cars++; //increase the number of cars } //Custom destructor Car::~Car(){ number_of_cars--; //decrease the number of cars std::cout<< "Custom Destructor:" <<std::endl; std::cout<< "Power:"<< getPower()<<std::endl; std::cout<< "Price: "<< getPrice() <<std::endl; std::cout<< "Total Cars: "<< number_of_cars <<std::endl; } //methods double Car::getPower (){ return Power; } double Car::getPrice (){ return price; } /* *********Minha fução Showinfo() const = Não tá funcionando*********/ /*********************************************************************/ void Car::showinfo() const{ std::cout<< "Power:"<< Power <<std::endl; std::cout<< "Price: "<< this->price <<std::endl; std::cout<< "Total Cars: "<< number_of_cars <<std::endl; } //methods to enable cascaded call Car &Car::setPower (double pwr){ Power=pwr; return *this; } Car &Car::setPrice (double pric){ price=pric; return *this; } // defines initial value of mandatory_airbags unsigned int Car::number_of_cars = 0; // defines static member function unsigned int Car::getNumCars(){ return number_of_cars; } // Operator + overload Car Car::operator+ (const Car &obj){ Car temp; temp.Power = Power + obj.Power; temp.price = price + obj.price; return temp; } // Operator < overload bool Car::operator< (Car &obj){ double FoM1,FoM2; FoM1 = Power/price; FoM2 = obj.Power/obj.price; return ((FoM1<FoM2)?true:false); } // non-member function of << overloading ostream &operator<<( ostream & ost_obj, const Car & obj){ //OK ost_obj<< "The power of this car is: "<< obj.Power<<" and it costs: "<<obj.price <<endl; return ost_obj; } /* *********Minha fução abaixo no stod(Power e Price) Não tá funcionando*********/ /**********************************************************************************/ // non-member function of >> overloading istream &operator>>( istream & ist_obj, Car & obj){ string Power; getline(ist_obj, Power, ','); //obj.Power = stod(Power); string Price; getline(ist_obj, Price); //obj.price = stod(Price); return ist_obj; } /* *********Minha main //1- até 4- = Não tá funcionando*********/ /***************************************************************/ int main(){ Car BMW(382, 41245); Car Ferrari(710, 350000); cout<<"BMW: "<<BMW <<endl; cout<<"Ferrari: "<<Ferrari<<endl; //1- cout<<"\n1-\n"; //cout<<"Ferrari-BMW: "<<Ferrari-BMW<<endl; //cout<<"BMW*Ferrari: "<<BMW*Ferrari<<endl; //cout<<"Ferrari/BMW: "<<Ferrari/BMW<<endl; //2- cout<<"\n2-\n"; //BMW+=20; cout<<"BMW+=20: "<<BMW<<endl; //BMW-=20; cout<<"BMW-=20: "<<BMW<<endl; //BMW*=2; cout<<"BMW*=2:: "<<BMW<<endl; //BMW/=2; cout<<"BMW/=2:: "<<BMW<<endl; //3- cout<<"\n3-\n"; //cout<<"++BMW: "<<++BMW<<endl; //cout<<"--BMW: "<<--BMW<<endl; //cout<<"BMW++: "<<BMW++<<" BMW later"<<BMW<<endl; //cout<<"BMW--: "<<BMW--<<" BMW later"<<BMW<<endl; //4- cout<<"\n4-\n"; //cout<<"BMW==Ferrari?: "<<((BMW==Ferrari()?"TRUE":"FALSE")<<endl; //cout<<"BMW!=Ferrari?: "<<((BMW!=Ferrari()?"TRUE":"FALSE")<<endl; //cout<<"BMW<=Ferrari?: "<<((BMW<=Ferrari()?"TRUE":"FALSE")<<endl; //cout<<"BMW>Ferrari?: "<<((BMW>Ferrari()?"TRUE":"FALSE")<<endl; //*/ return 0; } Poderia me ajudar? Pois não estou conseguindo rodar o restante da main.
  2. Quem Poderia me ajudar nesta outra? Incluindo as informações de Para que possa compilar a main() abaixo; #include <iostream> #include <vector> using namespace std; int main(){ Car BMW(382, 41245); Car Ferrari(710, 350000); cout<<"BMW: "<<BMW<<endl; cout<<"Ferrari: "<<Ferrari<<endl; //1- cout<<"\n1-\n"; cout<<"Ferrari-BMW: "<<Ferrari-BMW<<endl; cout<<"BMW*Ferrari: "<<BMW*Ferrari<<endl; cout<<"Ferrari/BMW: "<<Ferrari/BMW<<endl; //2- cout<<"\n2-\n"; BMW+=20; cout<<"BMW+=20: "<<BMW<<endl; BMW-=20; cout<<"BMW-=20: "<<BMW<<endl; BMW*=2; cout<<"BMW*=2:: "<<BMW<<endl; BMW/=2; cout<<"BMW/=2:: "<<BMW<<endl; //3- cout<<"\n3-\n"; cout<<"++BMW: "<<++BMW<<endl; cout<<"--BMW: "<<--BMW<<endl; cout<<"BMW++: "<<BMW++<<" BMW later"<<BMW<<endl; cout<<"BMW--: "<<BMW--<<" BMW later"<<BMW<<endl; //4- cout<<"\n4-\n"; cout<<"BMW==Ferrari?: "<<((BMW==Ferrari()?"TRUE":"FALSE")<<endl; cout<<"BMW!=Ferrari?: "<<((BMW!=Ferrari()?"TRUE":"FALSE")<<endl; cout<<"BMW<=Ferrari?: "<<((BMW<=Ferrari()?"TRUE":"FALSE")<<endl; cout<<"BMW>Ferrari?: "<<((BMW>Ferrari()?"TRUE":"FALSE")<<endl; return 0; }
  3. #include <iostream> #include <vector> using namespace std; class Car{ double Power; //força double Price; //Preço public: static unsigned number_of_cars; //nº de Carros alocação static Car(double power, double price){ cout << "Construtor Car executado" << endl; Power = power; Price = price; number_of_cars++; cout << "Number of Cars :: " << number_of_cars << endl; } virtual ~Car() { number_of_cars--; cout << "Destrutor Car executado" << endl; cout << "Number of Cars :: " << number_of_cars << endl; } void setPower(double power){Power=power;} double getPower(){return Power;} void setPrice(double price){Price = price;} double getPrice(){return Price;} virtual void status() { cout << "Power :: " << getPower() << endl; cout << "Price :: " << getPrice() << endl; } virtual void load(unsigned number) = 0; virtual void unload(unsigned number) = 0; }; unsigned Car::number_of_cars = 0; // unsigned nº não negativos class PassengerCar: public Car { unsigned MaxPassengers; unsigned Passengers; public: PassengerCar(double power, double price, unsigned maxPassengers, unsigned passengers) : Car(power, price) { cout << "Construtor PassengerCar executado" << endl; MaxPassengers = maxPassengers; Passengers = passengers; } virtual ~PassengerCar(){ cout << "Destrutor PassengerCar executado" << endl; } void status() { cout << "PassengerCar" << endl; Car::status(); cout << "MaxPassengers :: " << MaxPassengers << endl; cout << "Passengers :: " << Passengers << endl; } void load(unsigned number) { //Carregue if (Passengers + number > MaxPassengers) { cout << "Invalid (overflow)" << endl; return; } Passengers += number; } void unload(unsigned number) { //Descarregue if (int(Passengers - number) < 0) { cout << "Invalid (underflow)" << endl; return; } Passengers -= number; } }; class Truck : public Car { string Typeofload; //tipo de Carga bool Loaded; // Carregado unsigned NumberofWheels; // nº de Rodas public: Truck(double power, double price, string typeofload, unsigned numberofWheels) : Car(power, price) { cout << "Construtor Truck executado" << endl; Typeofload = typeofload; Loaded = false; NumberofWheels = numberofWheels; } virtual ~Truck(){ cout << "Destrutor Truck executado" << endl; } void status() { cout << "Truck" << endl; Car::status(); cout << "Typeofload :: " << Typeofload << endl; cout << "Loaded :: " << Loaded << endl; cout << "NumberofWheels :: " << NumberofWheels << endl; } void load (unsigned number) { Loaded = true; } void unload(unsigned number) { Loaded = false; } }; int main() { vector<Car *> cars; cars.push_back(new PassengerCar(382, 41245, 4, 0)); cars.push_back(new Truck(450, 50000, "Soy", 8)); cars[0]->status(); cars[0]->load(2); cars[0]->load(3); cars[0]->status(); cars[0]->unload(1); cars[0]->unload(2); cars[0]->status(); cout<<endl; cars[1]->status(); cars[1]->load(0); cars[1]->status(); cars[1]->unload(0); cars[1]->status(); cout<<endl; delete cars[0]; cars[1]->status(); return 0; } Senhores projeto final. Muito obrigado a todos!! Quem Poderia me ajudar nesta outra? Para Main!! #include <iostream> #include <vector> using namespace std; int main(){ Car BMW(382, 41245); Car Ferrari(710, 350000); cout<<"BMW: "<<BMW<<endl; cout<<"Ferrari: "<<Ferrari<<endl; //1- cout<<"\n1-\n"; cout<<"Ferrari-BMW: "<<Ferrari-BMW<<endl; cout<<"BMW*Ferrari: "<<BMW*Ferrari<<endl; cout<<"Ferrari/BMW: "<<Ferrari/BMW<<endl; //2- cout<<"\n2-\n"; BMW+=20; cout<<"BMW+=20: "<<BMW<<endl; BMW-=20; cout<<"BMW-=20: "<<BMW<<endl; BMW*=2; cout<<"BMW*=2:: "<<BMW<<endl; BMW/=2; cout<<"BMW/=2:: "<<BMW<<endl; //3- cout<<"\n3-\n"; cout<<"++BMW: "<<++BMW<<endl; cout<<"--BMW: "<<--BMW<<endl; cout<<"BMW++: "<<BMW++<<" BMW later"<<BMW<<endl; cout<<"BMW--: "<<BMW--<<" BMW later"<<BMW<<endl; //4- cout<<"\n4-\n"; cout<<"BMW==Ferrari?: "<<((BMW==Ferrari()?"TRUE":"FALSE")<<endl; cout<<"BMW!=Ferrari?: "<<((BMW!=Ferrari()?"TRUE":"FALSE")<<endl; cout<<"BMW<=Ferrari?: "<<((BMW<=Ferrari()?"TRUE":"FALSE")<<endl; cout<<"BMW>Ferrari?: "<<((BMW>Ferrari()?"TRUE":"FALSE")<<endl; return 0; } Desde já agradeço!!!
  4. O meu cód. ficou assim, só que ainda tem alguns problemas, poderiam me ajudar?? Gratos pela ajuda!! #include <iostream> #include <vector> using namespace std; class Car{ double Power; //força double Price; //Preço public: static unsigned number_of_cars; //nº de Carros Car(double power, double price){ Power = power; Price = price; number_of_cars++; } virtual ~Car() { number_of_cars--; cout<<"\n Destrutor executado" ; } void setPower(double power){Power=power;} double getPower(){return Power;} void setPrice(double price){Price = price;} double getPrice(){return Price;} virtual void status() { cout << "Power :: " << getPower() << endl; cout << "Price :: " << getPrice() << endl; } virtual void load() = 0; virtual void unload() = 0; }; unsigned Car::number_of_cars = 0; class PassengerCar: public Car { unsigned MaxPassengers; unsigned passengers; public: PassengerCar(double Power, double Price, unsigned maxPassengers, unsigned Passengers): Car( Power, Price){ MaxPassengers = maxPassengers; passengers = Passengers; } virtual ~PassengerCar(){ cout<<"\n Destrutor executado" ; } void status() { cout << "PassengerCar" << endl; Car::status(); cout << "MaxPassengers :: " << MaxPassengers << endl; cout << "passengers :: " << passengers << endl; } void load(unsigned number) { } void unload(unsigned number) { } }; class Truck : public Car { string typeofload; //tipo de Carga bool Loaded; // Carregado unsigned NumberofWheels; // nº de Rodas public: Truck(double Power, double Price, string typload, bool loaded, unsigned numberofWheels) : Car(Power, Price){ typeofload = typload; Loaded = loaded; NumberofWheels = numberofWheels; } virtual ~Truck(){ cout<<"\n Destrutor executado" ; } void status() { cout << "Truck" << endl; Car::status(); cout << "typeofload :: " << typeofload << endl; cout << "Loaded :: " << Loaded << endl; } void load (unsigned number) { } void unload(unsigned number) { } }; int main() { vector<Car *> cars; cars.push_back(new PassengerCar(382, 41245, 4, 0)); cars.push_back(new Truck(450, 50000, "Soy", 8)); carr[0]->status(); carr[0]->load(2); carr[0]->load(3); carr[0]->status(); carr[0]->unload(1); carr[0]->unload(2); carr[0]->status(); cout<<endl; carr[1]->status(); carr[1]->load(0); carr[1]->status(); carr[1]->unload(0) carr[1]->status(); cout<<endl; cars[1]->status(); return 0; }
  5. #include <iostream> using namespace std; class Car{ double Power; //força double price; //Preço unsigned number_of_cars; //nº de Carros public: Car(double Power, double price, unsigned number_of_cars){} virtual ~Car(){ cout<<"\n Destrutor executado" ; } void setPower(double Power;){} double getPower(){return Power;} void setprice(double price;){} double getpreice(){return price;} virtual char(status[], load[], unload[]) // fiquei na dúvida se pode?? }; class PassengerCar: public Car { unsigned MaxPassengers; unsigned Passengers; public: PassengerCar(unsigned MaxPassengers, unsigned Passengers, double Power, double price, unsigned number_of_cars): Car(double Power, double price, unsigned number_of_cars){} virtual -PassengerCar(){ cout<<"\n Destrutor executado" ;} virtual char(status[], load[], unload[]) // fiquei na dúvida se pode?? }; class Truck : public Car { string typeofload; //tipo de Carga bool Loaded; // Carregado unsigned numberofWheels; // nº de Rodas public: Truck(string typeofload, bool Loaded, unsigned numberofWheels, double Power, double price, unsigned number_of_cars): Car(double Power, double price, unsigned number_of_cars){} virtual ~Truck(){ cout<<"\n Destrutor executado" ;} virtual char(status[], load[], unload[]) // fiquei na dúvida se pode?? }; int main() { Vector<Car *> cars; cars.push_back(new PassengerCar(382, 41245, 4, 0)); cars.push_back(new Truck(450, 50000, "Soy", 8)); carr[0]->status(); carr[0]->load(2); carr[0]->load(3); carr[0]->status(); carr[0]->unload(1); carr[0]->unload(2); carr[0]->status(); cout<<endl; carr[1]->status(); carr[1]->load(0); carr[1]->status(); carr[1]->unload(0) carr[1]->status(); cout<<endl; cars[1]->status(); return 0; } è neste formato só que tenho algumas dúvidas
  6. @arfneto load() é carregue, unload() é descarregue. Fala isso para o professor!! hahahah
  7. Para Main, em baixo; Int main() { Vector<Car *> cars; cars.push_back(new PassengerCar(382, 41245, 4, 0)); cars.push_back(new Truck(450, 50000, "Soy", 8)); carr[0]->status(); carr[0]->load(2); carr[0]->load(3); carr[0]->status(); carr[0]->unload(1); carr[0]->unload(2); carr[0]->status(); cout<<endl; carr[1]->status(); carr[1]->load(0); carr[1]->status(); carr[1]->unload(0) carr[1]->status(); cout<<endl; cars[1]->status(); return 0; Obrigado!!!

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...