Im trying to solve a problem in C++ but because im a begginer i dont know how to do it !
The problem is this if you can help me :) :
By using the below C++ code create the proper constructor functions about the classes "car" & "truck". Each function must pass the appropriate arguments to the parent class of vehicle.Additionally, the function car should initialize the passengers when creating an object. The truck class should initialize the loadlimit when creating an object.
The statement of objects with the car () and truck () will be as follows:
car ob(passengers, wheels, range);
truck ob(loadlimit, wheels, range);
#include <iostream>
using namespace std;
class vehicle{
int num_wheels;
int range;
public:
vehicle(int w, int r){num_wheels = w; range = r;}
void showv(){
cout << "Wheels: " << num_wheels << endl;
cout << "Range: " << range << endl;
}
};
class car : public vehicle {
int passengers;
public:
void show(){
void showv();
cout << "Passengers: " << passengers << endl;
}
};
class truck : public vehicle {
int loadlimit;
public:
void show(){
void showv();
cout << "Loadlimit: " << loadlimit << endl;
}
};
int main(){
car c(5, 4, 500);
truck t(3000, 12, 1200);
cout << "Car: " << endl;
c.show();
cout << "Truck: " << endl;
t.show();
return 0;
}
Class Car and Truck does not have constructors that take required parameters and pass to the base class's constructor. they should be like this:
class car : public vehicle {
int passengers;
public:
car(int w,int r,int p): vehicle(w,r), passengers(p){}
void show(){
void showv();
cout << "Passengers: " << passengers << endl;
}
};
class truck : public vehicle {
int loadlimit;
public:
truck(int r, int w, int l):vehicle(r,w),loadlimit(l){}
void show(){
void showv();
cout << "Loadlimit: " << loadlimit << endl;
}
};
Base member initialisation
Car Constructor:
car(int a, int b, int c) : vehicle(a,b),passengers(c){}; //initialiser list
Truck Constructor:
truck(int g, int h, int j):vehicle(g,h),loadlimit(j){}
You need to add a Contractor to car and truck
class car : public vehicle {
int passengers;
public:
car(int p) : vehicle(int w, int r) // Call the superclass constructor in the subclass'
{
passengers = p;
}
void show()
{
showv();
cout << "Passengers: " << passengers << endl;
}
};
The same thing for Truck
Simple Solution,
car::car(int w,int r,int p)
{
passengers = p;
vehicle::vehicle(w,r);
}
Related
#include <iostream>
using namespace std;
class Student
{
protected:
long studentID;
public:
void setStudentID(long s_){ studentID = s_; }
Student(): studentID(0){}
long get_StudentID(){ return studentID; }
};
class Exam : public Student
{
protected:
float mark;
public:
void setMark(float m_){ mark = m_; }
Exam(): mark(0){}
float getMark(){ return mark; }
};
class Sports : public Student
{
protected:
float score;
public:
void setScore(float s_){ score = s_; }
Sports(): score(0){}
float getScore(){ return score; }
};
class Result: public Student, public Exam, public Sports
{
private:
float Total;
public:
float getTotal(){ return getMark() * getScore(); }
void display();
};
void Result::display()
{
cout << "Student ID = " << get_StudentID() << endl;
cout << "Exam Mark = " << getMark() << endl;
cout << "Sports Score = " << getScore() << endl;
cout << "Total Mark = " << getTotal();
}
int main()
{
Result st1;
st1.display();
return 0;
}
I wrote this code in Code::Blocks and it's incomplete yet , but this error that says "refrence to get_StudentID is ambigous" confused me; What's wrong with it?
Should I delete using namespace and insert std:: before all (cout, cin & endl) statments?
You are misunderstanding inheritance. For example, the class Exam with this line
class Exam : public virtual Student
is publicly inheriting from Student. Public inheritance should only be used in an IS-A relationship. In this case, Exam is not a Student so you shouldn't use public inheritance. Same goes for Sports.
"Call is ambiguous" means that when you call get_StudentID from Result the compiler can't decide between Exam::get_StudentID or Sports::get_StudentID which they both inherit from Student.
Instead:
Remove the inheritance, don't use inheritance anywhere here. I would completely remake this and make a Student class with Exam and Sports member objects (which can become vector of objects later if needed) and call the needed functions from the member objects instead. As a general tip, avoid multiple inheritance unless completely necessary.
This is an easy fix
remove Student from Results base class
make Student public virtual from both Exam and Sports class
#include <iostream>
using namespace std;
class Student
{
protected:
long studentID;
public:
void setStudentID(long s_){ studentID = s_; }
Student(): studentID(0){}
long get_StudentID(){ return studentID; }
};
class Exam : public virtual Student
{
protected:
float mark;
public:
void setMark(float m_){ mark = m_; }
Exam(): mark(0){}
float getMark(){ return mark; }
};
class Sports : public virtual Student
{
protected:
float score;
public:
void setScore(float s_){ score = s_; }
Sports(): score(0){}
float getScore(){ return score; }
};
class Result: public Exam, public Sports
{
private:
float Total;
public:
float getTotal(){ return getMark() * getScore(); }
void display();
};
void Result::display()
{
cout << "Student ID = " << get_StudentID() << endl;
cout << "Exam Mark = " << getMark() << endl;
cout << "Sports Score = " << getScore() << endl;
cout << "Total Mark = " << getTotal();
}
int main()
{
Result st1;
st1.display();
return 0;
}
Godbolt: https://godbolt.org/z/4f5n8M5of
When I tried to convert it the class result can't get the data from class marks even if I made the data variables of marks public I don't know why. I also declared a object of class marks in result and then tried to access it but failed again I am new to coding so don't know all the syntax correctly your help will be of great use
#include <string.h>
#include <iostream>
using namespace std;
class student {
private:
int rl;
char nm[20];
public:
void read();
void display();
};
class marks : public student {
protected:
int s1;
int s2;
int s3;
public:
void getmarks();
void putmarks();
};
class result : public marks {
private:
int t;
float p;
char div[10];
public:
void process();
void printresult();
};
void student::read() {
cout << "enter Roll no and Name " << endl;
cin >> rl >> nm;
}
void student::display() {
cout << "Roll NO:" << rl << endl;
cout << "name : " << nm << endl;
}
void marks ::getmarks() {
cout << "enter three subject marks " << endl;
cin >> s1 >> s2 >> s3;
}
void marks ::putmarks() {
cout << "subject 1:" << s1 << endl;
cout << "subject 2 :" << s2 << endl;
cout << "subject 3:" << s3 << endl;
}
void result::process() {
t = s1 + s2 + s3;
p = t / 3.0;
p >= 60 ? strcpy(div, "first")
: p >= 50 ? strcpy(div, "second")
: strcpy(div, "third");
}
void result::printresult() {
cout << "total = " << t << endl;
cout << "per = " << p << "%" << endl;
cout << "div = " << div << endl;
}
int main(){
result x;
x.read();
x.getmarks();
x.process();
x.display();
x.putmarks();
x.printresult();
}
#include<iostream>
#include<string.h>
using namespace std;
class marks // parent class
{
protected:
int s1;
int s2;
int s3;
public:
void getmarks();
void putmarks();
};
class student : public marks // child class
{
private:
int rl;
char nm[20];
public:
void read();
void display();
};
class result : public marks// child class
{
private:
int t;
float p;
char div[10];
public:
void process();
void printresult();
};
void student::read()
{
cout<<"enter Roll no and Name "<<endl;
cin>>rl>>nm;
}
void student:: display()
{
cout <<"Roll NO:"<<rl<<endl;
cout<<"name : "<<nm<<endl;
}
void marks ::getmarks()
{
cout<<"enter three subject marks "<<endl;
cin>>s1>>s2>>s3;
}
void marks ::putmarks()
{
cout <<"subject 1:"<<s1<<endl;
cout<<"subject 2 :"<<s2<<endl;
cout <<"subject 3:"<<s3<<endl;
}
void result::process()
{
t= s1+s2+s3;
p = t/3.0;
p>=60?strcpy(div,"first"):p>=50?strcpy(div, "second"): strcpy(div,"third");
}
void result::printresult()
{
cout<<"total = "<<t<<endl;
cout<<"per = "<<p<<"%"<<endl;
cout<<"div = "<<div<<endl;
}
int main()
{
result x;
student y;
y.read();
x.getmarks();
x.process();
y.display();
x.putmarks();
x.printresult();
}
I have been trying to compile this code. It has a class called books which and other genre of books inherit from it. However, when i compile the program it keeps saying Book is an inaccessible base of Police. Then it shows red lines under the first two add_book calls in the main where they add new Police.
I dont see where there is lack of access in my code?
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Book{
public:
virtual double calc_price()const;
Book(string t, string a, int pg, bool bs)
: title(t), author(a), pages(pg), bestseller(bs){}
virtual ~Book(){};
virtual void display() const;
protected:
string title;
string author;
int pages;
bool bestseller;
};
void Book::display() const {
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "Number of pages: " << pages << endl;
cout << "Bestseller: "; if(bestseller==true){ cout << "Yep"
<< endl; } else {cout << "Nope" << endl;
cout << "Price: " << calc_price() << endl; }
}
double Book::calc_price() const {
if(bestseller==true){ return (pages*0.3 + 50); }
else { return (pages*0.3); }}
class Roman : public Book {
public:
Roman(string t, string a, int pg, bool bs, bool bio)
: Book(t,a,pg,bs), biography(bio){}
virtual ~Roman();
virtual void display()const override;
protected:
bool biography;
};
void Roman::display() const{
Book::display();
cout << "Ce roman ";
if(biography==true){ cout << "is a biography. " << endl;
} else { cout << "isn't a biography. " << endl; }
}
class Police : Roman {
public:
Police(string t, string a, int pg, bool bs, bool bio)
: Roman(t,a,pg,bs,bio){}
virtual double calc_price() const {
double price;
price = Book::calc_price() - 10;
if(price < 0) { return 1; } else { return price; }}
virtual~Police();
};
class Fantasy : public Livre {
public:
Fantasy(string t, string a, int pg, bool bs)
: Book(t,a,pg,bs){}
virtual ~Fantasy();
virtual double calc_price() const {
return (Book::calc_price() + 30); }
};
class Library{
public:
void display() const;
void add_book(Book* l);
void empty_stock();
private:
vector<Book*> books;
};
void Library::add_book(Book* b){
books.push_back(b);
}
void Library::display() const {
for(size_t i(0); i < books.size(); ++i){
books[i]->display();
} }
void Library::empty_stock(){
for(size_t i(0); i < books.size(); ++i){
delete books[i]; } books.clear();
}
int main()
{
Library l;
l.add_book(new Police("Dogs of Baskerville", "A.C.Doyle", 221, false,false));
l.add_book(new Police("Le Parrain ", "A.Cuso", 367, true, false));
l.add_book(new Roman("Book3", "I. Calvino", 283, false, false));
l.add_book(new Roman ("Geronimoe memories", "S.M. Barrett", 173, false, true));
l.add_book(new Fantasy ("European rivers", "C. Osborne", 150, false));
l.display();
l.empty_stock();
return 0;
}
Change class Police : Roman in class Police : public Roman.
If public is not specified, the Roman will be a private base class for Police.
Change this line
class Police : Roman {
to
class Police : public Roman {
The program is working in this way. But I need to change the price system. I need to use priceRead and priceWrite.
priceRead (): double
priceWrite (in _price: double): void
priceRead: price is a function to read the property.
priceWrite (double): price property is a function of writing.
Sorry for bad english. :(
#include <iostream>
#include <string.h>
using namespace std;
class shoes{
protected:
int size;
string colour;
char sex;
string brand;
public: shoes(int _size, string _colour, char _sex, string _brand)
:size(_size),colour(_colour), sex(_sex), brand(_brand)
{cout << "shoe configurator" << endl;}
~shoes(){ }
void shoesInf();
};
class storage{
protected:
int number;
int maxsize;
int minsize;
public: storage(int _number, int _maxsize, int _minsize)
:number(_number),maxsize(_maxsize),minsize(_minsize)
{cout << "store configurator" << endl;}
~storage(){ }
void storageInf();
};
void shoes::shoesInf()
{
cout << "Size :" << size << endl;
cout << "Colour :" << colour << endl;
cout << "Sex :" << ((sex=='M')?"Man":"Woman") << endl;
cout << "Brand:" << brand << endl;
}
void storage::storageInf()
{
cout << "Number :" << number << endl;
cout << "Max size :" << maxsize << endl;
cout << "Min size :" << minsize << endl;
}
class Store: public storage{
private: int number;
int maxsize;
int minsize;
public: Store(int _number, int _maxsize, int _minsize)
:storage(_number,_maxsize,_minsize)
{cout << "storage INF" << endl;}
};
class Nike: public shoes{
private: double price;
public: Nike(int _size, string _colour, char _sex, string _brand,
double _price):shoes(_size,_colour,_sex,_brand),price(_price)
{cout << "Shoes INF" << endl;}
void shoesInf()
{
((shoes*)this)->shoesInf();
cout << "Price :" << price << endl;
}
};
class Lacoste: public shoes{
private: double price;
public: Lacoste(int _size, string _colour, char _sex, string _brand,
double _price):shoes(_size,_colour,_sex,_brand),price(_price)
{cout << "Shoes INF" << endl;}
void shoesInf()
{
((shoes*)this)->shoesInf();
cout << "Price:" << price<< endl;
}
};
int main(int argc, char** argv) {
Store*sh=new Store(1,50,15);
sh->storageInf();
Nike *sh2=new Nike(38,"Blue",'F',"Nike",100);
sh2->shoesInf();
Lacoste *sh3=new Lacoste(40,"Yellow",'M',"Lacoste",350);
sh3->shoesInf();
return 0;
}
class Shoes
{
private:
int size;
string colour;
char sex;
string brand;
double price;
public:
Shoes(int _size, string _colour, char _sex, string _brand, double _price) :
size(_size), colour(_colour), sex(_sex), brand(_brand), price(_price)
{}
void writePrice(double newPrice)
{
price = newPrice;
}
void readPrice()
{
cout << price << endl;
}
};
Notes:
All shoes have a price, so why not just put it in the base class?
Brand is defined by the brand string, all shoes are the same, so no need to derive a new class for each brand.
No need to have an empty destructor.
Also, without this redefinition of price when deriving, you don't have to redefine shoeInf everytime you want to derive from Shoe.
One more thing: Start class names with CapitalLetters (in that style!).
#include <iostream>
using namespace std;
class Vehicle{
protected:
string type;
int wheels;
bool engine; // number of engines in vehicle
public:
Vehicle(string t, int w,bool e):
type(t), wheels(w), engine(e){};
void setType(string t) {type = t;}
void setWheels(int w) {wheels = w;}
void setEngine(int e) {engine = e;} // number of engines, 0 - False.
string getType(){return type;}
int getWheels() {return wheels;}
bool getEngine() {cout << "1 - Has Engine | 0 - No Engine"; return engine;}
};
class Auto:public Vehicle {
private:
string brand;
int year;
public:
Auto(string t, int w, bool e, string b, int y):
Vehicle(t,w,e), brand(b),year(y) {};
void setBrand(string b) {brand = b;}
void setYear(int y) {year = y;}
string getBrand() {return brand;}
int getYear() {return year;}
};
int main()
{
// This first segment of the program demonstrates the relationship
// between the base class and derived class through the use of
// a constructor.
Auto Spider360("Car",4,2,"Ferrari",2000);
cout << "Car type: " << Spider360.getType() << endl;
cout << "Number of wheels: " << Spider360.getWheels() << endl;
cout << " Has Engine: " << Spider360.getEngine() << "\n";
cout << "Brand: " << Spider360.getBrand() << endl;
cout << "Year: " << Spider360.getYear() << "\n\n";
// Now I use member functions directly to assign values to an object
Auto SuperAmerica;
return 0;
}
I am unable to declare the object Auto SuperAmerica; I get the following error: "No matching function call for Auto::Auto()" and for SuperAmerica, i do not want to use a constructor to set the values, I want to use my Set functions.
The error of
"No matching function call for Auto::Auto()"
means that you cannot instantiate the class in the way you wanted. If you want to create the object and then initialize its members later, using setters, then use a default constructor.