#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.
Related
for example, I have a base class Unit and Unit has heirs, for example Soldier, Vampire. They have their own lives and damage. Is it possible to display data Vampire, Soldier using a separate class for example StateOfUnits where there is an overload of the output operator.
something like :
base class :
class Unit {
private:
int hpLimit;
int dmg;
int hp;
std::string name;
public:
Unit(int hpLimit = 100, int dmg = 10, const std::string& name = "noname");
~Unit();
int getDamage() const;
int getHP() const;
int getHpLimit() const;
const std::string& getName() const;
};
class that displays statistics:
class StateOfUnits {
///may be some code...
};
std::ostream& operator<<(std::ostream& out, const Unit& unit) {
out << "HP of " << unit.getName() << " is : " << unit.getHP() << "/" << unit.getHpLimit() << std::endl;
out << "Damage of " << unit.getName() << " is : " << unit.getDamage() << std::endl;
return out;
}
main:
int main() {
hp dmg name
Vampire vampire(100,25,"Vampire");
Soldier soldier(120, 20, "Soldier");
cout << soldier << endl;
cout << vampire << endl;
return 0;
}
I hope I explained it clearly.
Yes, if StateOfUnit is a base class:
#include <iostream>
using namespace std;
struct StateOfUnit
{
int hp;
int dmg;
string name;
StateOfUnit(int hp, int dmg, string name)
: hp(hp), dmg(dmg), name(name)
{}
friend ostream& operator<<(ostream& os, const StateOfUnit& s)
{
return os << s.hp << '\t' << s.dmg << '\t' << s.name;
}
};
struct Soldier : StateOfUnit
{
using StateOfUnit::StateOfUnit;
};
struct Vampire : StateOfUnit
{
using StateOfUnit::StateOfUnit;
};
int main() {
Vampire vampire(100,25,"Vampire");
Soldier soldier(120, 20, "Soldier");
cout << soldier << endl;
cout << vampire << endl;
return 0;
}
This code is showing error when I make an instance of the class website. The instance has to use the explicit constructor which I have defined in the class definition. So I am passing a string value in it. This value is being received by an array of character type in the constructor which then initializes a pointer pointing to that array. Please avoid giving complex answers.
#include<iostream>
#include<conio.h>
using namespace std;
class Links
{
private:
char *linkname;
public:
Links()
{
cout << "Links default constructor called." << endl;
};
Links(char n[]):linkname(n)
{
cout << "Links parameterized constructor called." << endl;
};
char getlinkname()
{
return *linkname;
}
void setlinkname(char n[])
{
linkname = n;
}
};
class Webpage
{
private:
double width;
double height;
Links link1;
Links link2;
public:
Webpage()
{
cout << "Webpage default constructor called." << endl;
};
Webpage(double w, double h) :width(w), height(h)
{
cout << "Webpage parameterized constructor called." << endl;
};
double getheight()
{
return height;
}
double getwidth()
{
return width;
}
void setheight(double h)
{
height = h;
}
void setwidth(double w)
{
width = w;
}
};
class Website
{
private:
char *name;
Webpage webpage1{24.5,37.2};
Webpage webpage2;
Webpage webpage3{10,18.7};
Webpage webpage4;
public:
Website()
{
cout << "Website default constructor called." << endl;
};
Website(char n[]):name(n)
{
cout << "Website parameterized constructor called." << endl;
};
char getname()
{
return *name;
}
void setname(char n[])
{
name = n;
}
};
int main()
{
Website w1("www.google.com");
_getch();
return 0;
}
String literals are 'const', try this:
class Website
{
private:
const char *name;
Webpage webpage1{24.5,37.2};
Webpage webpage2;
Webpage webpage3{10,18.7};
Webpage webpage4;
public:
Website()
{
cout << "Website default constructor called." << endl;
};
Website(const char* n):name(n)
{
cout << "Website parameterized constructor called." << endl;
};
const char* getname()
{
return name;
}
void setname(const char* n)
{
name = n;
}
};
i want to know how can i declare a variable that contain boolean in it in the class section like as
class account {
char itemName[50];
double actualPrice;
bool empty= false;
public:
void create_account();
void displayRecord() const;
void drawLine3(int n, char symbol);
};
void
account::create_account()
{
do{
cout << "Type the name of Item " << endl;
cin.getline(itemName, 50);
}while(itemName!=empty);
cout << "Actual price :" << endl;
cin >> actualPrice;
cout << endl;
cout << "Item Name :-> " << itemName << endl;
cout << "Actual Price :->" << actualPrice << endl;
}
You should have a constructor to initialize class members:
class account {
char itemName[50];
double actualPrice;
bool empty;
public:
account() : empty(false) {} // this initializes the 'empty' variable to 'false'.
void create_account();
void displayRecord() const;
void drawLine3(int n, char symbol);
};
You cannot, except if your compiler is C++-11 compliant (in that case the syntax you wrote is correct), otherwise you should add a constructor that sets inital values for the variables that need an initialization:
class account {
public:
account() : itemName(""), actualPrice(0.0), empty(true) {}
[...]
};
PS: if your compiler is GCC or CLANG you can enable C++-11 compatibility through the command line switch -std=c++11
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 {
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);
}