This question already has answers here:
Undefined reference to static variable c++
(3 answers)
Closed 2 years ago.
I created a Class Student with instance data members
like studentname, studentaddress, studentrollno
i declared private static data member CollegeName that could be shared among all instances of class.
used promptfunction to take user input from the console
next functions to display student details.
Meanwhile resulting in below errors :
Meanwhile codes goes here:
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
int studentrollno;
string studentName;
string studentAddress;
static string CollegeName;
public:
void setRollNo(int rollno) {
studentrollno = rollno;
}
int getRollNo() {
return studentrollno;
}
void setName(string name) {
studentName = name;
}
string getName() {
return studentName;
}
void setAddress(string address) {
studentAddress = address;
}
string getAddress() {
return studentAddress;
}
static void setCollegeName(string collegename) {
CollegeName = collegename;
}
static string getCollegeName() {
return CollegeName;
}
void displayStudentDetails(); // member functions declare inside class Meanwhile it is defined
outside
static void show() {
cout << "this is static function " << endl;
}
};
// member functions define outside Student class
void Student :: displayStudentDetails() {
cout << "Student Name : " << getName() << " Student RollNo : " << getRollNo() << "Student Address
: " << getAddress() << "CollegeName: "<< getCollegeName() << endl;
}
void promptValues(Student &student) {
cout << "Enter the student Details " << endl;
cout << "Enter the details about student objects " << endl;
cout << endl;
int studentrollno;
string studentname , studentaddress ;
string collegename;
cout << "Enter the RollNo of the student " << endl;
cin >> studentrollno;
cout << "Enter the Name of the student " << endl;
cin >> studentname;
cout << endl;
cout << "Enter the address of the student " << endl;
cin >> studentaddress;
cout << endl;
cout << "Enter the collegeName of the student " << endl;
cin >> collegename;
student.setRollNo(studentrollno), student.setName(studentname), student.setAddress(studentaddress),
student.setCollegeName(collegename);
}
int main() {
Student student1, student2 , student3 , student4 , student5 ;
Student studentarrays[5] = { student1, student2, student3, student4, student5 };
Student studentmodel[5];
for (int i = 0; i < 5; i++) {
Student model;
model = studentarrays[i];
promptValues(model);
studentmodel[i] = model;
}
for (Student studentdetails : studentmodel) {
studentdetails.displayStudentDetails();
}
Student::show();
return 0;
}
You have omited the definition of the static member Student::CollegeName. It is only declared in the class, now you should define it after the class declaration with this:
std::string Student::CollegeName;
For more information see https://en.cppreference.com/w/cpp/language/static
Related
The code compiles it does not read staff.txt. Not sure how to fix in the code. the error must be in reading the staff.txt of the code. Does anyone have an idea? output is in the image provided. Background on the question:
You are tasked to create 2 ADTs: customers and staff. Each customer will have a first and last name, an indication if the customer is enrolled in the “preferred” program, the car they are driving, and the amount of money owed to the company. On the other hand, each staff member will have a first and last name, the car they are driving, and their salary. In a real-world problem, there would obviously be more records associated with each staff member and customer.
ODU rentals would like you to automatically process txt files and print the information to the console screen. When you perform this process, ensure that your member variables are private. In addition, be sure to use getter and setter functions to manipulate and print your data. You may print your data in any format as long as it is organized. An example of correct output can be seen below.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
class Customer {
string fname;
string lname;
int enroll;
string car;
float amount;
public:
//Defining the customer ADT
Customer();
void setFirstName(string fnm);
void setLastName(string lnm);
void setEnroll(int e);
void setCarName(string cnm);
void setAmount(float amt);
string getFirstName();
string getLastName();
int getEnroll();
string getCarName();
float getAmount();
void printData();
};
Customer::Customer() {
fname = lname = car = "";
enroll = 0;
amount = 0;
}
void Customer::setFirstName(string fnm) {
fname = fnm;
}
void Customer::setLastName(string lnm) {
lname = lnm;
}
void Customer::setEnroll(int e) {
enroll = e;
}
void Customer::setCarName(string cnm) {
car = cnm;
}
void Customer::setAmount(float amt) {
amount = amt;
}
string Customer::getFirstName() {
return(fname);
}
string Customer::getLastName() {
return(lname);
}
int Customer::getEnroll() {
return(enroll);
}
string Customer::getCarName() {
return(car);
}
float Customer::getAmount() {
return(amount);
}
void Customer:: printData() {
cout << left << setw(25) << getFirstName()+" "+getLastName() << left <<setw(15) << getEnroll();
cout << left << setw(15) << getCarName() << left << setw(15) << fixed << setprecision(2) << getAmount() << endl;
}
class Staff {
string fname;
string lname;
string car;
float salary;
public:
//Defining staff ADT
Staff();
void setFirstName(string fnm);
void setLastName(string lnm);
void setCarName(string cnm);
void setSalary(float sal);
string getFirstName();
string getLastName();
string getCarName();
float getSalary();
void printData();
};
Staff::Staff() {
fname = lname = car = "";
salary = 0;
}
void Staff::setFirstName(string fnm) {
fname = fnm;
}
void Staff:: setLastName(string lnm) {
lname = lnm;
}
void Staff::setCarName(string cnm) {
car = cnm;
}
void Staff::setSalary(float sal) {
salary = sal;
}
string Staff::getFirstName() {
return(fname);
}
string Staff::getLastName() {
return(lname);
}
string Staff::getCarName() {
return(car);
}
float Staff::getSalary() {
return(salary);
}
void Staff:: printData() {
cout << left << setw(25) << getFirstName()+" "+getLastName() << left << setw(15);
cout << getCarName() << left << setw(15) << fixed << setprecision(2) << getSalary() <<endl;
}
//Main function
//Read txt files
int main() {
string fnm, lnm, carnm;
float amt, sal;
int e, i=0, recno;
ifstream fstaff("staff.txt");
if(!fstaff) {
cout << "File can not be opened" << endl;
exit(1);
}
fstaff >> recno;
Staff s[recno];
cout << left<<setw(25) << "Name" << left << setw(15) << "Car" << left << setw(15) << "Salary" <<endl;
cout << "____________________________________________________________________________" << endl;
for(i=0; i<recno; i++) {
fstaff>>fnm>>lnm>>carnm>>sal;
s[i].setFirstName(fnm);
s[i].setLastName(lnm);
s[i].setCarName(carnm);
s[i].setSalary(sal);
s[i].printData();
}
fstaff.close();
ifstream fcust("customers.txt");
if(!fcust) {
cout << "File can not be opened"<<endl;
exit(1);
}
fcust >> recno;
Customer c[recno];
cout << endl << endl;
cout << left << setw(25) << "Name" << left << setw(15) << "Preferred" << left << setw(15) << "Car" << left << setw(15) << "Bill" << endl;
cout <<"____________________________________________________________________________"<<endl;
for(i=0; i<recno; i++) {
fcust >> fnm >> lnm >> e >> carnm >>amt;
c[i].setFirstName(fnm);
c[i].setLastName(lnm);
c[i].setEnroll(e);
c[i].setCarName(carnm);
c[i].setAmount(amt);
c[i].printData();
}
fcust.close();
return 1;
}
Code Output
This question already has answers here:
Undefined reference to a static member
(5 answers)
Closed 1 year ago.
I'm trying to print the employee count after each created object but I'm getting an error like this: undefined reference to `Employee::numberofEmployees'. How can I solve this little problem? Any ideas? Btw I'm using a local class.
the purpose of the numberofEmployees variable is to store the information on the number of employee objects created/instantiated so far.
#include <iostream>
#include <string>
using namespace std;
class Employee{
public:
string name;
string surname;
int year;
double salary;
static int numberofEmployees;
Employee(int yil,string isim,string soyisim): year(yil),name(isim),surname(soyisim){
numberofEmployees++;
}
Employee(){
name = "not-set";
year = 0;
surname = "not-set";
salary = 0.0;
}
void calculateSalary(){
salary = 2310 + 2310 * year * 12 / 100.0;
}
void printInfo(){
cout << name << " " << surname << " " << "(" << year << ")" << " " << salary << "TL/month" << endl;
}
};
int main()
{
Employee person1(4,"Berk","Kandemir");
cout << Employee::numberofEmployees << endl;
Employee person2(8,"Esat","Kandemir");
cout << Employee::numberofEmployees << endl;
Employee person3(20,"Paul","Walker");
cout << Employee::numberofEmployees << endl;
person1.calculateSalary();
person2.calculateSalary();
person3.calculateSalary();
person1.printInfo();
person2.printInfo();
person3.printInfo();
return 0;
}
You have to initialize your static data member outside the class using scope resolution operator something like this.
int Employee::numberofEmployees = 0;
This shows that you are accessing a static variable of class Employee and then initializing it.
If you have a static variable within a class, you are supposed to initialise it outside of the class, ie in a .cpp file. From C++17, however, you are allowed to initialise it in-class, in-line, using the inline keyword.
Code:
#include <string>
#include <iostream>
using namespace std;
class Employee
{
public:
string name;
string surname;
int year;
double salary;
static inline int numberofEmployees = 0;
Employee(int yil, string isim, string soyisim) : year(yil), name(isim), surname(soyisim)
{
numberofEmployees++;
}
Employee()
{
name = "not-set";
year = 0;
surname = "not-set";
salary = 0.0;
}
void calculateSalary()
{
salary = 2310 + 2310 * year * 12 / 100.0;
}
void printInfo()
{
cout << name << " " << surname << " "
<< "(" << year << ")"
<< " " << salary << "TL/month" << endl;
}
};
int main()
{
Employee person1(4, "Berk", "Kandemir");
cout << Employee::numberofEmployees << endl;
Employee person2(8, "Esat", "Kandemir");
cout << Employee::numberofEmployees << endl;
Employee person3(20, "Paul", "Walker");
cout << Employee::numberofEmployees << endl;
person1.calculateSalary();
person2.calculateSalary();
person3.calculateSalary();
person1.printInfo();
person2.printInfo();
person3.printInfo();
return 0;
}
Or keep your code and add:
int Employee::numberofEmployees = 0;
After your class.
Output:
1
2
3
Berk Kandemir (4) 3418.8TL/month
Esat Kandemir (8) 4527.6TL/month
Paul Walker (20) 7854TL/month
#include <iostream>
#include <vector>
using namespace std;
class Flight;
class Time {
private :
int hour;
int minute;
public :
Time(int hour,int minute){
this->hour = hour;
this->minute = minute;
};
int getHour(){
return hour;
}
int getMinute(){
return minute;
}
};
class Passenger{
private:
string name;
int age;
public :
Passenger(string name , int age){
this->name = name;
this->age = age;
}
void printDetails(){
cout << "Name: " << name << "\t";
cout << "Age: " << age <<"\t";
}
friend void addPassenger(Passenger *p,int num,Flight f);
friend Flight;
};
class Flight {
private :
string id;
string destination;
Time *depart;
Time *arrival;
vector<Passenger> passengerList;
Passenger *pass;
public :
Flight(string id, string destination, Time *t, Time *a){
this->id = id;
this->destination = destination;
depart = t;
arrival = a;
id = 3;
};
void printInfo(){
cout<< "Flight Number : " << id << endl;
cout<< "Destination : " << destination << endl;
cout<< "Desparture : " << depart->getHour() << ":" << depart->getMinute()<< endl;
cout<< "Arrival : " << arrival->getHour() << ":" << arrival->getMinute() << endl;
}
void printList(){
cout << pass->name[0];
}
friend class Passenger;
friend void addPassenger(Passenger *p,int num,Flight f);
};
void addPassenger(Passenger *p,int num,Flight f){
// for(int i=0;i<num;i++){
// f.passengerList.push_back(p[i]);
f.pass->name = p->name;
// }
}
int main(){
int num_passenger;
int temp_age;
string temp_name;
vector<int> passenger_age;
vector<string> passenger_name;
Time t(2,4);
Time t2(2,3);
Flight ff("3","4",&t,&t2);
cout<< "Enter the number of passenger" << endl;
cin>> num_passenger;
cout<< endl;
Passenger *pas[num_passenger];
for(int i=0;i < num_passenger; i++){
cout<< "Enter the name of adult "<< i+1 << endl;
cin>> temp_name;
passenger_name.push_back(temp_name);
cout<< "Enter the age of adult "<< i+1 << endl;
cin>> temp_age;
passenger_age.push_back(temp_age);
}
for(int p=0; p < num_passenger; p++){
pas[p] = new Passenger(passenger_name[p],passenger_age[p]);
}
addPassenger(*pas,2,ff);
ff.printList();
return 0;
}
I need to pass array object of Passenger Class into private array object of Passenger Class that inside object of Flight Class through function addPassenger.
Everything compile fine, but I can't get printList(object Flight) work, it simply jumps out and terminates the console.
Sorry it quite complicated due to requirement of project.
Hope to have some helps, Thanks.
Problem 1
The main problem is at line
f.pass->name = p->name;
The source of the problem is that the member variable pass of Flight is not initialized properly and then you go on to use f.pass as though it is a valid pointer.
Problem 2
The function addPassenger takes a Flight object as input. When you call the function, the function gets a copy of the original Flight object. Any changes made to the Flight object in addPassenger are to the local copy, not the object from main.
With the following changes, I was able to run your program without any problem.
Changed the member of variable of Flight to:
string id;
string destination;
Time *depart;
Time *arrival;
vector<Passenger> passengerList;
Removed the member varible pass.
Changed the signature of addPassenger to.
void addPassenger(std::vector<Passenger> const& plist, Flight& f);
and changed its implementation to
void addPassenger(std::vector<Passenger> const& plist, Flight& f)
{
for(auto& p : plist ){
f.passengerList.push_back(p);
}
}
Changed the implementation of Flight::printList to:
void printList(){
for(auto& p : passengerList ){
cout << p.name << endl;
}
}
Changed the type of pas in main to:
std::vector<Passenger> pas;
Other things needed to be changed to account for these changes. Here's the complete program:
#include <iostream>
#include <vector>
using namespace std;
class Flight;
class Time {
private :
int hour;
int minute;
public :
Time(int hour,int minute){
this->hour = hour;
this->minute = minute;
};
int getHour(){
return hour;
}
int getMinute(){
return minute;
}
};
class Passenger{
private:
string name;
int age;
public :
Passenger(string name , int age){
this->name = name;
this->age = age;
}
void printDetails(){
cout << "Name: " << name << "\t";
cout << "Age: " << age <<"\t";
}
friend void addPassenger(std::vector<Passenger> const& plist, Flight& f);
friend Flight;
};
class Flight {
private :
string id;
string destination;
Time *depart;
Time *arrival;
vector<Passenger> passengerList;
public :
Flight(string id, string destination, Time *t, Time *a){
this->id = id;
this->destination = destination;
depart = t;
arrival = a;
id = 3;
};
void printInfo(){
cout<< "Flight Number : " << id << endl;
cout<< "Destination : " << destination << endl;
cout<< "Desparture : " << depart->getHour() << ":" << depart->getMinute()<< endl;
cout<< "Arrival : " << arrival->getHour() << ":" << arrival->getMinute() << endl;
}
void printList(){
for(auto& p : passengerList ){
cout << p.name << endl;
}
}
friend class Passenger;
friend void addPassenger(std::vector<Passenger> const& plist, Flight& f);
};
void addPassenger(std::vector<Passenger> const& plist, Flight& f)
{
for(auto& p : plist ){
f.passengerList.push_back(p);
}
}
int main(){
int num_passenger;
int temp_age;
string temp_name;
vector<int> passenger_age;
vector<string> passenger_name;
Time t(2,4);
Time t2(2,3);
Flight ff("3","4",&t,&t2);
cout<< "Enter the number of passenger" << endl;
cin>> num_passenger;
cout<< endl;
std::vector<Passenger> pas;
for(int i=0;i < num_passenger; i++){
cout<< "Enter the name of adult "<< i+1 << endl;
cin>> temp_name;
passenger_name.push_back(temp_name);
cout<< "Enter the age of adult "<< i+1 << endl;
cin>> temp_age;
passenger_age.push_back(temp_age);
}
for(int p=0; p < num_passenger; p++){
pas.push_back(Passenger(passenger_name[p],passenger_age[p]));
}
addPassenger(pas, ff);
ff.printList();
return 0;
}
See it working at http://ideone.com/Chttoe.
I am new to c++ and am working on a project for class. I know I that some of my functions are not correct. I am trying to get to a point to where I can at least see the output to continue working on it. I have included a brief description of that I am trying to do.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Employee
{
protected:
char* name[50];
public:
Employee()
{
}
Employee(char* name)
{
strcpy(name, name);
}
char* getName()
{
return *name;
}
void setName(char* name)
{
name = name;
}
/*virtual ~Employee()
{
delete[] name;
}*/
virtual void print() = 0;
};
class HourlyEmployee : public Employee
{
private:
float HourlySalary;
public:
HourlyEmployee()
{
HourlySalary = 0;
}
HourlyEmployee(char* name, float HourlySalary)
{
name = name;
HourlySalary = HourlySalary;
}
double getHourlySalary()
{
return HourlySalary;
//cout << "What is your Hourly Salary" << endl;
//cin >> HourlySalary;
}
void setHourlySalary(double HourlySalary)
{
}
void print()
{
cout << "Hourly Employee Name: " << &name << endl
<< "Salary: " << &HourlySalary << "per hour" << endl;
}
};
class SalariedEmployee : public Employee
{
private:
float MonthlySalary;
public:
SalariedEmployee()
{
MonthlySalary = 0;
}
SalariedEmployee(char* name, float MonthlySalary)
{
}
double getMonthlyySalary()
{
return MonthlySalary;
//cout << "What is your Hourly Salary" << endl;
//cin >> MonthlySalary;
}
void setMonthlySalary(double MonthlySalary)
{
}
void print()
{
cout << "Hourly Employee Name: " << name << endl
<< "Salary: " << MonthlySalary << "per month" << endl;
}
};
int main() {
SalariedEmployee* S = new SalariedEmployee();
SalariedEmployee S1("Joe Bob", '4500');
HourlyEmployee* H = new HourlyEmployee();
HourlyEmployee H1("Jim Bob", '20');
S1.print();
H1.print();
delete S, H;
system("pause");
return 0;
}
From the description of your exercise I concluded that you're asking for something like this:
#include <iostream>
using namespace std;
class Employee
{
protected:
char name[50];
public:
Employee()
{
}
Employee(char* name)
{
strncpy_s(this->name, 49, name, 49);
}
char* getName()
{
return this->name;
}
void setName(char *name)
{
strncpy_s(this->name, 49, name, 49);
}
virtual void print() = 0;
};
class HourlyEmployee : public Employee
{
private:
float hourlySalary;
public:
HourlyEmployee()
{
hourlySalary = 0;
}
HourlyEmployee(char* name, float HourlySalary)
{
strncpy_s(this->name, 49, name, 49);
this->hourlySalary = HourlySalary;
}
double getHourlySalary()
{
return hourlySalary;
//cout << "What is your Hourly Salary" << endl;
//cin >> HourlySalary;
}
void setHourlySalary(double HourlySalary)
{
this->hourlySalary = HourlySalary;
}
void print()
{
cout << "Hourly Employee Name: " << this->name << endl
<< "Salary: " << hourlySalary << " per hour" << endl;
}
};
class SalariedEmployee : public Employee
{
private:
float MonthlySalary;
public:
SalariedEmployee()
{
MonthlySalary = 0;
}
SalariedEmployee(char* name, float MonthlySalary)
{
strncpy_s(this->name, 49, name, 49);
this->MonthlySalary = MonthlySalary;
}
double getMonthlyySalary()
{
return MonthlySalary;
//cout << "What is your Hourly Salary" << endl;
//cin >> MonthlySalary;
}
void setMonthlySalary(double MonthlySalary)
{
this->MonthlySalary = MonthlySalary;
}
void print()
{
cout << "Hourly Employee Name: " << name << endl
<< "Salary: " << MonthlySalary << " per month" << endl;
}
};
int main()
{
Employee * employee[2];
employee[0] = new SalariedEmployee("Joe Bob", 4000);
employee[1] = new HourlyEmployee("Jim Bob", 20);
for (int i = 0; i < 2; i++)
{
employee[i]->print();
}
for (size_t i = 0; i < 2; i++)
{
delete employee[i];
}
system("pause");
return 0;
}
First off,your name variable's gotta be of type char[50],not *char[50]. So I used strncpy(or the safe function strncpy_s) to copy the name in our constructor. Since you're dealing with chars,you cannot assign it like you did in some parts of your code,like this name = name. And I used this->name because the variable names are identical. Next off,in your main function you gotta have the Employee class and then assign it to an HourlyEmployee and SalariedEmployee , because those are the principles of polymorphism. Hope that I have helped you and if you have any questions,don't hesitate to ask.
`
Im trying to create a percent savings program for a supermarket with 3 inherited classes (PersonData -> CustomerData -> PreferredCustomer). The entire program has to follow this UML diagram:
I am having problems with the third class; specifically initializing values in the first constructor of the class. Im getting an error message in visual studio: "redefinition of formal parameter Pa" and "redefinition of formal parameter "dl".
This is the constructor with the errors:
PreferredCustomer(string aLname, string aFname, string aAddress,
string aCity, string aState, int aZip, string aPhone, int cn,
bool ml, double Pa, double dl) // constructor 1
{
double Pa;
double dl;
}
The parameters in this third child constructor have been overloaded from the parent constructor. Arguments cn, ml, Pa, dl have all been added into this parameter.
I'm not sure why i am getting these errors? Also, should i be creating a virtual function or using pointers? Thank you for any help.
My program source code (for reference):
#include <iostream>
#include <string>
using namespace std;
class PersonData // PersonData parent class defined
{
private:
string lastName;
string firstName;
string address;
string city;
string state;
string phone;
int zip;
public:
PersonData() // Default Constructor initialization
{
lastName = " ";
firstName = " ";
address = " ";
city = " ";
state = " ";
zip = 0;
phone = " ";
}
PersonData(string aLname, string aFname, string aAddress, string aCity, string aState, int aZip, string aPhone) // Constructor 1
{
lastName = aLname;
firstName = aFname;
address = aAddress;
city = aCity;
state = aState;
zip = aZip;
phone = aPhone;
}
// Accesor Functions
string getLastName() const
{
return lastName;
}
string getFirstName() const
{
return firstName;
}
string getAddress() const
{
return address;
}
string getCity() const
{
return city;
}
string getState() const
{
return state;
}
int getZip() const
{
return zip;
}
string getPhone() const
{
return phone;
}
// Mutator Functions
void setLastName(string aLname)
{
lastName = aLname;
}
void setFirstName(string aFname)
{
firstName = aFname;
}
void setAddress(string aAddress)
{
address = aAddress;
}
void setCity(string aCity)
{
city = aCity;
}
void setState(string aState)
{
state = aState;
}
void setZip(int aZip)
{
zip = aZip;
}
void setPhone(string aPhone)
{
phone = aPhone;
}
};
class CustomerData :public PersonData // CustomerData child class of PersonData base class
{
private:
int customerNumber;
bool mailingList;
public:
CustomerData() // Default constructor
{
customerNumber = 0;
mailingList = 0;
}
CustomerData(int cNum, bool mailL) // Constructor 1
{
setCustomerNumber(cNum);
setMailingList(mailL);
}
// Accessor Functions for child class
int getCustomerNumber() const
{
return customerNumber;
}
bool getMailingList() const
{
if (mailingList != 0)
{
cout << "On Mailing List!: ";
return mailingList;
}
else (mailingList == 0);
{
cout << "Not on mailing list!: ";
return mailingList;
}
}
// Mutator Functions for child class
void setCustomerNumber(int cNum)
{
customerNumber = cNum;
}
void setMailingList(bool mailL)
{
mailingList = mailL;
}
};
class PreferredCustomer :public CustomerData // child class of CustomerData child class
{
private:
double purchasesAmount;
double discountLevel;
public:
PreferredCustomer() // default constructor
{
purchasesAmount = 0;
discountLevel = 0;
}
PreferredCustomer(string aLname, string aFname, string aAddress,
string aCity, string aState, int aZip, string aPhone, int cn,
bool ml, double Pa, double dl) // constructor 1
{
double Pa;
double dl;
}
// Mutator Functions
void setPurchasesAmount(double Pa)
{
purchasesAmount = Pa;
}
// Accessor Functions
double getPurchasesAmount() const
{
return purchasesAmount;
}
double getDiscountLevel() const
{
return discountLevel;
}
void addPurchase(double P) const
{
}
};
int main()
{
CustomerData Cdata; // for access of child class functions
PersonData Pdata; // for access of parent class functions
PreferredCustomer PCdata; // for access of preferred customer class functions
string temp1; // Temporary variable for string values
int temp2, // Temporary variable for integer values
max = 100, // For-loop maximum loop value
i; // i variable
bool temp3; // Temporary variable for bool values
for (i = 1; i <= max; i++)
{
// Input Data
cout << "Please input first Name: ";
getline(cin, temp1);
Pdata.setFirstName(temp1);
cout << "Please input last Name: ";
getline(cin, temp1);
Pdata.setLastName(temp1);
cout << "Please input address: ";
getline(cin, temp1);
Pdata.setAddress(temp1);
cout << "Please input city: ";
getline(cin, temp1);
Pdata.setCity(temp1);
cout << "Please input state: ";
getline(cin, temp1);
Pdata.setState(temp1);
cout << "Please input Zip code: ";
cin >> temp2;
Pdata.setZip(temp2);
cin.ignore(); // discards unread char from cin
cout << "Please input Phone Number: ";
getline(cin, temp1);
Pdata.setPhone(temp1);
cout << "Enter 1 to be included in mail list," << endl;
cout << "Enter 0 to not be included in mail list: ";
cin >> temp3;
Cdata.setMailingList(temp3);
Cdata.setCustomerNumber(i); // set customer number
cout << endl << endl;
cout << "Name: " << Pdata.getFirstName() << ", " << Pdata.getLastName() << " \n";
cout << "Customer Number: " << Cdata.getCustomerNumber() << "\n";
cout << "Address: " << Pdata.getAddress() << "\n";
cout << "City: " << Pdata.getCity() << "\n";
cout << "State: " << Pdata.getState() << "\n";
cout << "Phone: " << Pdata.getPhone() << "\n";
cout << "Zip: " << Pdata.getZip() << "\n";
cout << Cdata.getMailingList() << "\n";
cout << endl << endl;
cin.ignore(); // discards unread char from cin
}
char c;
cin >> c;
return 0;
}
A constructor's purpose is to take the parameters the are passed in and construct the object from the class declaration and initialize members as appropriate. It not only has to initialize the class but also all the base classes as well, this can be done by passing parameters to the base constructor(s).
In
PreferredCustomer(string aLname, string aFname, string aAddress,
string aCity, string aState, int aZip, string aPhone, int cn,
bool ml, double Pa, double dl) // constructor 1
{
double Pa;
double dl;
}
You aren't initializing anything
You are declaring two local parameters that match (and conflict with) the parameters passed in
You don't call the base's constructors (how most of the work is done)
PreferredCustomer(...) :CustomerData(...)
CustomerData(...) :PersonData(...)
You don't initialize this class's members
purchasesAmount = pa;
discountLevel = dl;