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;
}
};
Related
Here is the code where i used composition. I removed the non-relevant functions to make it little easier to understand .When i run this code using parametrized constructor, it work fine. But if i use default constructor while initilizing it does not work, the code terminate in between.
#include <iostream>
using namespace std;
class link
{
const char* name;
public:
link() :name("null")
{};
link(const char n[]) :name(n)
{};
~link()
{
cout << "destructor called " << endl;
};
};
class webpage
{
private:
double height;
double width;
link* links;
public:
webpage() :height(10), width(10),links(new link[1])
{};
webpage(double hw, link* hyperlinks) :height(hw), width(hw), links(hyperlinks)
{ };
webpage(double h, double w, link* hyperlinks) :height(h), width(w), links(hyperlinks)
{ };
~webpage()
{
delete [] links;
cout << "page destructor called " << endl;
};
void showdata(int linkno)
{
cout << "height: " << height << endl;
cout << "width: " << width << endl;
cout << "links " << endl;
for (int i = 0; i < linkno; i++)
{
cout << "link #" << i + 1 << " = " << links[i].getname() << endl;
}
}
};
class website
{
private:
const char* name;
webpage* wpgs;
public:
website() :name("null"),wpgs(new webpage[1])
{};
website(const char n[], webpage* page) :name(n), wpgs(page)
{};
~website()
{
delete[] wpgs;
cout << "website destructor " << endl;
};
void showdata(int linkno, int pageno)
{
cout << "Website name: " << name << endl;
for (int j = 0; j < pageno; j++)
{
cout << "Webpage #" << j + 1 << " : " << endl;
wpgs[j].showdata(linkno);
}
}
};
int main(int argc, char* argv[])
{
link* link1=new link[2] {{"maha"},{"saira"}};
link* link2=new link[3] {{"areeb"},{"aima"},{"umair"}};
link* link3=new link[2] {{"ahmad"},{"azra"}};
link* link4=new link[4] {{"usama"},{"tyabba"},{"ali"},{"hamza"}};
webpage* page=new webpage[4] {{2,link1},{3.2,5.2,link2},{4,1,link3},{42,13,link4}};
website site("my website",page);
site.showdata(2,4);
}
the above code work fine but if i use default constructor like in below code
int main()
{
link* links;
webpage* page;
website site("website", page);
site.showdata(1, 1);
}
now it won't work. the code with terminate after a bit.
and generate error
error1 'links': unreferenced local variable
error2 uninitialized local variable 'page' used
How can I inlitialize using default constructor
please help.
All the conditions implemented in question are demand of problem statement.
You aren't using any constructor because links and page are pointers. Pointers do not have constructors.
Maybe you want this?
webpage page;
website site("website", &page);
Now because page is not a pointer the webpage default constructor will be called.
Or looking at the rest of your code, probably this is correct.
webpage* page = new webpage[1];
website site("website", page);
new webpage[1] calls the webpage default constructor.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
below is my code:
class Pulley{
private:
int noOfTeeth;
public:
Pulley();
Pulley(int teeth);
~Pulley();
void show();
};
Pulley::Pulley()
{
cout << "Pulley constructor called!";
noOfTeeth = 0;
}
Pulley::Pulley(int teeth)
{
cout << "Pulley constructor called!";
noOfTeeth = teeth;
}
void Pulley::show()
{
cout << "\n\nNo of Teeths of Pulley: " << noOfTeeth;
}
Pulley::~Pulley()
{
}
class GearBox{
private:
char *transmission;
Pulley p;
public:
GearBox();
GearBox(char *trans, int pTeeth);
~GearBox();
void show();
};
GearBox::GearBox(): p()
{
cout << "Gearbox constructor called!";
transmission = NULL;
}
GearBox::GearBox(char *trans, int pTeeth): p(pTeeth)
{
cout << "Gearbox constructor called!";
if(trans != NULL)
{
transmission = new char[strlen(trans)+1];
strcpy(transmission, trans);
}
else
{
transmission = NULL;
}
}
void GearBox::show()
{
cout << "\n\nTransmission of vehicle: " << transmission;
p.show();
}
GearBox::~GearBox()
{
}
class Vehicle{
private:
char *model;
char *color;
GearBox g;
public:
Vehicle();
Vehicle(char *mod, char *col, char *gr);
Vehicle(const Vehicle &vh);
~Vehicle();
void show();
};
Vehicle::Vehicle(): g()
{
cout << "Vehicle constructor called!";
model = NULL;
color = NULL;
}
Vehicle::Vehicle(char *mod, char *col, char *gr): g(gr)
{
cout << "Vehicle constructor called!";
if(mod != NULL)
{
model = new char[strlen(mod)+1];
strcpy(model, mod);
}
else
{
model = NULL;
}
if(col != NULL)
{
color = new char[strlen(col)+1];
strcpy(color, col);
}
else
{
color = NULL;
}
}
void Vehicle::show()
{
cout << "\n\nModel of Vehicle: " << model;
cout << "\n\nColor of Vehicle: " << color;
g.show();
}
int main()
{
Pulley p(20);
GearBox g("Manual", p);
Vehicle V("Honda", "Black", g);
V.show();
system("PAUSE");
}
Now, when I run this code I get a lots of error, I don't know what are those and how to resolve them. The one error I understood is of Copy Constructor, so can anyone explain me that how can I write the copy constructor for pointer to characters transmission, model and color. Also tell me how do I resolve other errors. Thanks a lot.
Try like this:
#include <string>
#include <iostream>
class Pulley{
private:
int noOfTeeth;
public:
Pulley(int teeth = 0);
void show();
};
Pulley::Pulley(int teeth)
: noOfTeeth(teeth)
{
std::cout << "Pulley constructor called!";
}
void Pulley::show()
{
std::cout << "\n\nNo of Teeths of Pulley: " << noOfTeeth;
}
class GearBox{
private:
std::string transmission;
Pulley p;
public:
GearBox(std::string trans = std::string(), Pulley p = Pulley());
void show();
};
GearBox::GearBox(std::string trans, Pulley p)
: transmission(std::move(trans))
, p(std::move(p))
{
std::cout << "Gearbox constructor called!";
}
void GearBox::show()
{
std::cout << "\n\nTransmission of vehicle: " << transmission;
p.show();
}
class Vehicle{
private:
std::string model;
std::string color;
GearBox g;
public:
Vehicle();
Vehicle(std::string mod = "", std::string col = "", GearBox gr = GearBox());
void show();
};
Vehicle::Vehicle(std::string mod,std::string col, GearBox gr)
: model(std::move(mod))
, color(std::move(col))
, g(std::move(gr))
{
std::cout << "Vehicle constructor called!";
}
void Vehicle::show()
{
std::cout << "\n\nModel of Vehicle: " << model;
std::cout << "\n\nColor of Vehicle: " << color;
g.show();
}
int main()
{
Pulley p(20);
GearBox g("Manual", p);
Vehicle V("Honda", "Black", g);
V.show();
// system("PAUSE");
}
This is my code of a example program. The problem im getting is that when I add a new item to the shoppingCart array, it makes a new copy of the shoppingCart array with the present shoppingindex. How can I add all these items into the same shoppingCart array.
#include <iostream>
#include <iomanip>
using namespace std;
class Product{
private:
protected:
static int shoppingindex;
string shoppingcart[10];
public:
Product()
{
}
void displayShoppingCart() const
{
cout << endl<<endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << " Shopping Cart \n";
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
for(int i=0;i<10;i++)
{
// if(shoppingcart[i].length()<=1)
// {
//
// }
// else
{
cout << "\nShopping Item " <<"("<< i+1 << ")" << " - " << shoppingcart[i] << endl;
//cout << shoppingindex << endl;
}
}
}
};
class DairyProduct:public Product{
private:
protected:
public:
DairyProduct()
{
}
void AddDairyProduct(string productName)
{
shoppingcart[shoppingindex]=productName;
shoppingindex++;
}
};
class Beverages:public Product{
private:
protected:
public:
Beverage()
{
}
void AddBeverage(string productName)
{
shoppingcart[shoppingindex]=productName;
shoppingindex++;
}
};
class PersonalCare:public Product{
private:
protected:
public:
PersonalCare()
{
}
void AddPersonalCare(string productName)
{
//if conditions in all of them to match with the given dataset
shoppingcart[shoppingindex]=productName;
shoppingindex++;
}
};
class ShoppingCart{
public:
Product *object;
DairyProduct *dairy;
Beverages *bevg;
PersonalCare *personal;
ShoppingCart()
{
object = new Product; //deep copy
dairy=new DairyProduct;
bevg=new Beverages;
personal = new PersonalCare;
}
~ShoppingCart()
{
}
};
int Product::shoppingindex=0;
int main()
{
ShoppingCart user;
user.bevg->AddBeverage("Tea");
user.bevg->displayShoppingCart();
user.personal->AddPersonalCare("Shampoo");
user.personal->displayShoppingCart();
user.dairy->AddDairyProduct("Milk");
user.dairy->displayShoppingCart();
}
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//In this section I create the class HotelRoom
class HotelRoom
{
protected:
HotelRoom(){}
char room_Number[4];
char* guestName;
double rate;
public:
HotelRoom(char Num[], double daily, char* name);
HotelRoom(const HotelRoom &);
~HotelRoom();
int Get_Capacity();
int Get_Status();
double Get_Rate();
const char* Get_Number();
const char* Get_Guest();
int Change_Status(int Stat);
};
HotelRoom::HotelRoom(char Num[], double daily, char* name)
{
strcpy_s(room_Number, 4, Num);
guestName = new char[strlen(name) + 1];
strcpy_s(guestName, 20, name);
rate = daily;
}
HotelRoom::HotelRoom(const HotelRoom& room_r)
{
strcpy_s(room_Number, room_r.room_Number);
guestName = new char[strlen(room_r.guestName) + 1];
strcpy_s(guestName, 20, room_r.guestName);
rate = room_r.rate;
}
HotelRoom::~HotelRoom()
{
cout << endl << endl;
cout << "Decunstructor Activated." << endl;
delete[] guestName;
}
double HotelRoom::Get_Rate()
{
return rate;
}
const char* HotelRoom::Get_Number()
{
return room_Number;
}
const char* HotelRoom::Get_Guest()
{
return guestName;
}
class DerivedGuestRoom : public HotelRoom
{
public:
DerivedGuestRoom(int max, int stat, int nights);
DerivedGuestRoom(const DerivedGuestRoom&);
~DerivedGuestRoom();
protected:
int capacity;
int status = 0; //0 if unoccupied
int days;
};
DerivedGuestRoom::DerivedGuestRoom(int max, int stat, int nights) : capacity(max), status(stat), days(nights)
{
cout << "data members set";
}
DerivedGuestRoom::~DerivedGuestRoom()
{
cout << "\ndecunstrucor";
}
class DerivedMeetingRoom : public HotelRoom
{
public:
DerivedMeetingRoom(int seat, int stat);
DerivedMeetingRoom(const DerivedMeetingRoom&);
~DerivedMeetingRoom();
protected:
int seats;
int status; //1 if booked for meeting 0 otherwise
};
DerivedMeetingRoom::DerivedMeetingRoom(int seat, int stat) : seats(seat), status(stat)
{
cout << "data members set";
}
DerivedMeetingRoom::~DerivedMeetingRoom()
{
cout << "\ndecunstrucor";
}
void Display_info(HotelRoom&);
HotelRoom* Create_Hotel();
int main()
{
cout << setprecision(2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);
HotelRoom* Hotel_Rooms[200];
int count = 0;
char response;
cout << endl;
cout << "Do you want to enter information about a hotel room?(Y/N): ";
response = cin.get();
cin.get();
while (toupper(response) == 'Y' && count < 10)
{
Hotel_Rooms[count] = Create_Hotel();
++count;
cout << endl;
cout << "Do you want to enter information about a hotel room?(Y/N): ";
response = cin.get();
cin.get();
}
//Display the accounts created
for (int i = 0; i < count; ++i)
Display_info(*Hotel_Rooms[i]);
for (int i = 0; i < count; ++i)
delete Hotel_Rooms[i];
cout << endl;
system("PAUSE");
return 0;
}
void Display_info(HotelRoom& room)
{
cout << "\n\nGuest's Name: " << room.Get_Guest() << endl << endl;
cout << "\nYour room number is " << room.Get_Number() << endl << endl;
cout << "\nDaily rate is: " << room.Get_Rate() << endl << endl;
cout << endl;
cout << endl;
}
HotelRoom* Create_Hotel()
{
//These are the variables that will be holding data then passed through objects
char roomNumber[4];
char guestName[20];
double dailyRate = 89.00;
HotelRoom* room_ptr;
//This portion asks for user input
cout << "\nEnter Guest information\n\n";
cout << "Enter the room number: ";
cin.getline(roomNumber, 4);
cout << "Enter the guest's name: ";
cin.getline(guestName, 20);
cout << endl;
cin.get(); //Clears input buffer
room_ptr = new HotelRoom(roomNumber, dailyRate, guestName);
return room_ptr;
}
I took out HotelRoom(); from parent class constructor and I removed from the child class constructors. I Now only have this error:
LearningOOP.exe has triggered a breakpoint.
Which I have never encountered this so not sure what it means.
You declared a default constructor:
class HotelRoom
{
public:
HotelRoom();
}
and there is no implementation for that default constructor method. You can change to:
class HotelRoom
{
public:
HotelRoom() {}
}
or implement HotelRoom:HotelRoom { } in your cpp file.
You haven't defined your default constructor for the HotelRoom class.
class HotelRoom
{
protected:
char room_Number[4];
char* guestName;
double rate;
public:
HotelRoom(); //< Not defined!
HotelRoom(char Num[], double daily, char* name);
HotelRoom(const HotelRoom &);
~HotelRoom();
int Get_Capacity();
int Get_Status();
double Get_Rate();
const char* Get_Number();
const char* Get_Guest();
int Change_Status(int Stat);
};
An "Unresolved external symbol" linker error means that you've declared and used a piece of code in your application, but you haven't defined it before using it.
EDIT (Based on your follow up comment below):
You can't take the constructor out because it's needed in other parts of your code. See this line in your code:
DerivedGuestRoom::DerivedGuestRoom(int max, int stat, int nights) : HotelRoom(), capacity(max), status(stat), days(nights)
// ^---------^
// |
// Using the default constructor.
DerivedMeetingRoom::DerivedMeetingRoom(int seat, int stat) : HotelRoom(), seats(seat), status(stat)
// ^---------^
// |
// And again here!
You either need to remove implement the default constructor for the HotelRoom class, or add parameters to your DerivedMeetingRoom and DerivedGuestRoom class constructors so that you can use the HotelRoom(char Num[], double daily, char* name) constructor.
#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.