Pointer to an object in inheritance - c++

Here I'm not getting correct output, Here I used inheritance concept, but I don't know, how to call method using object of pointer type.
Please some one give me some solution.
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
class person
{
private:
char *name,*blood,*dob;
int height,weight;
public:
static int count;
person()
{
strcpy(name,"Name");
strcpy(blood,"Blood");
height=0;
weight=0;
}
~person()
{
}
friend class person_info;
};
class person_info
{
private:
char *add,*tel,*lic,*insu;
public:
void getdata(person *obj,int n);
void display(person *obj);
};
void person_info::getdata(person *obj,int n)
{
for(int i=0;i<n;i++)
{
cin.ignore();
cout<<"Enter Name Of the Person : ";
cin.getline(obj->name,30);
cout<<"Enter Blood group Of the Person : ";
cin.getline(obj->blood,30);
cout<<"Enter date of birth of the Person : ";
cin.getline(obj->dob,30);
cout<<"Enter Height of the Person : ";
cin>>obj->height;
cout<<"Enter Weight of the Person : ";
cin>>obj->weight;
cout<<"Enter Address Of the Person : ";
cin.getline(this->add,30);
cout<<"Enter Insurance no. Of the Person : ";
cin.getline(this->insu,30);
cout<<"Enter Telephone no Of the Person : ";
cin.getline(this->tel,30);
cout<<"Enter License no the Person : ";
cin.getline(obj->blood,30);
}
}
void person_info::display(person *obj)
{
cout<<setw[10]<<"Name"<<setw[10]<<"Address"<<setw[10]<<"D. O. B."<<setw[3]<<"Blood G."<<setw[4]<<"Height"<<setw[10]<<"Weight"<<setw[10]<<"Insrn No."<<setw[10]<<"Tele No."<<setw[11]<<"Licence No.";
cout<<setw[10]<<obj->name<<setw[10]<<this->add<<setw[10]<<obj->dob<<setw[3]<<obj->blood<<setw[4]<<obj->height<<setw[10]<<obj->weight<<setw[10]<<this->insu<<setw[10]<<this->tel<<setw[11]<<this->lic;
}
int main()
{
int ch=0,i=0,n;
do
{
cout<<"1.getdata"<<endl;
cout<<"2.display data"<<endl;
cout<<"Enter choice";
cin>>ch;
person_info *p[2]; \\I think this part is not correct
p[2]=new person_info(); \\I think this part is not correct
person *p1[2]; \\I think this part is not correct
p1[2]=new person(); \\I think this part is not correct
switch(ch)
{
case 1:
cout<<"Enter No. Entries to be Entered :";
cin>>n;
p[2]->getdata(p1[2],n);
break;
case 2:
for(int j=0;j<i;j++)
{
p[i]->display(p1[i]);
}
break;
}
}while(ch!=3);

You neither initialized your members char *name,*blood,*dob;, nor you allocated memory for them. I recommend to use std::string instead of char*.
#include <string>
class person
{
private:
std::string name;
std::string blood;
public:
person()
: name( "Name" )
, blood( "Blood" )
{}
person( const char *n, const char *b )
: name( n )
, blood( b )
{}
};
Note: strcpy(name,"Name"); copies the string "Name" to the memory referenced by char *name, but you never allocate any dynamic memory for name. name is not initialized and undefined.

Related

How to relate user with a book (Library system) C++

I am doing a university project and I am making a Library System.
It has a few restrictions I cannot use what we haven't been taught (so no vectors or std:: things).
Now, I have some classes made.
(1) Date
(2) Book
(3) Student
(4) Library //Haven't made it yet
Now, when a student/user issues a book their account number is being saved in "Book" class and so is the return_date.
Now, I wish to make a pointer or use this-> operator or something that will be in Student class but point to a Book class.
So, when Student wants to see which books they have currently issued will be displayed.
I am not sure how to do that.
Should I make a Library class, give it an array of "Book" class by composition and then make a member function in "Student/User" class and display it there?
I really want to use new/delete keyword, this, or pointers to do this. It's not compulsory but it will help me understand those concepts.
The following are my Book, Student and Date classes.
Date Class:
class Date
{
int Day; //1-31 based on month
int Month; //1-12
int Year; //any year
int checkDay(int );
int Late_Days;
public:
static const int Days_per_Month[13];
Date()
{
Day=0;
Month=0;
Year=0;
}
Date (int dy, int mn, int yr)
{
if (mn>0 && mn <=12)
Month=mn;
else
{
Month=1;
cout<<"Month "<<mn<<" invalid. Set to month 1"<<endl;
}
Year=yr;
Day=checkDay(dy);
}
void setDay(int d) {Day=d;}
void setMonth(int m) {Month=m;}
void setYear(int y) {Year=y;}
void setLateDay(int LD) {Late_Days=LD;}
int getDay() {return Day;}
int getMonth() {return Month;}
int getYear() {return Year;}
int getLateDay() {return Late_Days;}
void Print_Date()
{
cout<<getDay()<<"/"<<getMonth()<<"/"<<getYear()<<endl;
//cout<<Day<<"/"<<Month<<"/"<<Year<<endl;
}
void Update_Date()
{
cout<<"\nEnter Day: ";
cin>>Day;
cout<<"Enter Month: ";
cin>>Month;
cout<<"Year: ";
cin>>Year;
}
void increment_date(int num)
{
int day;
int month_new;
// Day=Day+num;
setDay(getDay()+num);
if( getDay()>Days_per_Month[getMonth()] )
{
day=getDay()-Days_per_Month[getMonth()];
setDay(day);
setMonth(getMonth()+1);
if(Days_per_Month[getMonth()]>12)
{
month_new=1;
setMonth(month_new);
setYear(getYear()+1);
}
}
Print_Date();
}
const int Date:: Days_per_Month[13]={0,31,28,31,30,31,30,31, 31, 30, 31, 30, 31};
int Date::checkDay(int testday) //returntype classname :: funcname (parameteres)
{
//static const int Days_per_Month[13]={0,31,28,31,30,31,30,31, 31, 30, 31, 30, 31};
if(testday > 0 && testday <= Days_per_Month[Month])
return testday;
if ( Month==2 && testday==29 && (Year%400==0 || (Year%4==0 && Year%100!=0)) ) //for leap year
return testday;
cout<<"Day "<<testday<<" invalid. Set to day 1."<<endl;
return 1;
}
Book Class
class Book
{
string Title;
string Author;
unsigned long int ISBN;
int Year_of_Publication;
int Library_Code;
string Category;
string Status;
unsigned int Account_Number; //if issued
int Copies_of_Book;
Date Return_Date;
public:
static int Copy_Number;
Book()
{
Title=" ";
Author=" ";
ISBN=0;
Year_of_Publication=0;
Library_Code=0;
Category=" ";
Status=" ";
Account_Number=0;
Copies_of_Book=0;
Return_Date.setDay(0);
Return_Date.setMonth(0);
Return_Date.setYear(0);
}
Book(string t, string a,unsigned long int isbn, int yop, int libcode, string c, string s, unsigned int an, int cob)
{
setTitle(t);
setAuthor(a);
setISBN(isbn);
setYOP(yop);
setLibraryCode(libcode);
setCategory(c);
setStatus(s);
setAccountNum(an);
setCopiesBook(cob);
}
void setTitle(string T) {Title=T;}
void setAuthor(string A) {Author=A;}
void setISBN(unsigned long int isbn){ISBN=isbn;}
void setYOP(int yop) {Year_of_Publication=yop;}
void setLibraryCode(int libcode) {Library_Code=libcode;}
void setCategory(string c) {Category=c;}
void setStatus(string s) {Status=s;}
void setAccountNum(unsigned int an) {Account_Number=an;}
void setCopiesBook(int cob) {Copies_of_Book=cob;}
void setCopyNumber(int cn) {Copy_Number=cn;}
string getTitle() {return Title;}
string getAuthor() {return Author;}
unsigned long int getISBN() {return ISBN;}
int getYOP() {return Year_of_Publication;}
int getLibraryCode() {return Library_Code;}
string getCategory() {return Category;}
string getStatus() {return Status;}
unsigned int getAccountNum() {return Account_Number;}
int getCopiesBook() {return Copies_of_Book;}
int getCopyNumber() {return Copy_Number;}
void Input_New_Book()
{
cout<<"\nEnter Title: ";
cin>>Title;
cout<<"Enter Author: ";
cin>>Author;
cout<<"Enter ISBN: ";
cin>>ISBN;
cout<<"Enter Year of Publication: ";
cin>>Year_of_Publication;
cout<<"Enter Library Code: ";
cin>>Library_Code;
cout<<"Enter Category: ";
cin>>Category;
cout<<"Enter Total Copies Number: ";
cin>>Copies_of_Book;
cout<<"Enter Status: ";
cin>>Status;
if(Status=="Issued")
{
cout<<"Enter Account Number: ";
cin>>Account_Number;
Return_Date.Update_Date();
}
}
void Display_Book_Info()
{
cout<<"\nTitle: "<<getTitle()<<endl;
cout<<"Author: "<<getAuthor()<<endl;
cout<<"ISBN: "<<getISBN()<<endl;
cout<<"Year of Publication: "<<getYOP()<<endl;
cout<<"Library Code: "<<getLibraryCode()<<endl;
cout<<"Category: "<<getCategory()<<endl;
cout<<"Total Copies: "<<getCopiesBook()<<endl;
cout<<"Status: "<<getStatus()<<endl;
if(getStatus()=="Issued")
{
cout<<"Account Number: "<<getAccountNum()<<endl;
cout<<"Return Date: ";
Return_Date.Print_Date();
}
}
void Update_Status()
{
unsigned int AN;
Display_Book_Info();
cout<<"Please enter the updated status of the book: ";
cin>>Status; //We cannot use string in an switch, so we are using if.
if (Status=="Lost")
{
cout<<"The book is lost."<<endl;
setAccountNum(0);
}
else if (Status=="Available")
{
cout<<"Book is available now to issue."<<endl;
setAccountNum(0); //This indicates it doesn't have nay account number.
}
else if(Status=="Issued")
{
cout<<"The book is in possession of someone."<<endl;
cout<<"Enter their account number:";
cin>>AN;
setAccountNum(AN);
}
else if (Status=="Order")
{
cout<<"The book is in process of being ordered."<<endl;
setAccountNum(0);
}
else
{
cout<<"Wrong entry!\nBook's Status is set to default. (Available)"<<endl;
Status="Available";
setAccountNum(0);
}
cout<<"Status updated successfully!"<<endl;
}
void Issue_Book(int day_num) //This day_num indicated number of days.
{
unsigned int an;
cout<<"Enter account number of User:";
cin>>an;
setAccountNum(an);
Return_Date.Update_Date();
cout<<"Book issued for "<<day_num<<" days."<<endl;
cout<<"Return Date: ";
Return_Date.Print_Date();
}
void ReIssue_Book(int day_num)
{
unsigned int an;
cout<<"Enter account number of User:";
cin>>an;
setAccountNum(an);
Return_Date.increment_date(day_num);
cout<<"Book issued for "<<day_num<<" more days."<<endl;
cout<<"Return Date: ";
Return_Date.Print_Date();
}
void Return_Book()
{
cout<<"Book returned from User: "<<getAccountNum()<<endl;
setAccountNum(0);
cout<<"Book returned successfully!"<<endl;
}
}; //end of class book
int Book::Copy_Number=0;
Student Class
class Student:public Book
{
string Name;
unsigned int Account_Number;
double Fine;
int Books_Issue;
public:
Student()
{
Name=" ";
Account_Number=0;
Fine=0;
Books_Issue=0;
}
void setName(string n) {Name=n;}
void setAccount_Number(unsigned int an) {Account_Number=an;}
void setFine(double f) {Fine=f;}
void setBooksIssue(int n) {Books_Issue=n;}
string getName() {return Name;}
unsigned int getAccount_Number() {return Account_Number;}
double getFine() {return Fine;}
int getBooksIssue() {return Books_Issue;}
void Input_Student_info()
{
cout<<"\nEnter Name: ";
cin>>Name;
cout<<"Enter Account Number: ";
cin>>Account_Number;
}
void Display_Student_info()
{
cout<<"Name: "<<getName()<<endl;
cout<<"Account Number: "<<getAccount_Number()<<endl;
cout<<"Total Fine: "<<getFine()<<endl;
cout<<"Books issued: "<<getBooksIssue()<<endl;
}
void Issue_a_book()
{
if(Books_Issue<=3)
{
Issue_Book(7); //This will issue a book and give it a 7 days return date time
Books_Issue++; //This will mean a book has been issued.
}
else
cout<<"Cannot issue book. You have reached your maximum limit.\nReturn a book in order to issue another."<<endl;
}
void Display_All_Books() //which are currently issued
{
for (int i=0 ; i<=getBooksIssue() ; i++)
{
}
}
};
Sorry for posting huge chunks of code. But some people say we should post the whole code, so this is why I posted all of it organized way.
The main question is:
How should I display issued books in a Student's class?
Should I make a Library class (as a parent class or child class, or use composition and arrays of Book class) first or at the very end?
Let me know what is the best way to do this.
Thank You so much.
It is better to have remove the user issue details from the book class...
Let the book class have only details about the book and methods to update and retrieve book details.
Add the user issue details into a new class which inherits the book class... This way is more modular and keeps the books as separate entities. Now you can make a linked list(as they are dynamic) or array of these classes, each holding a book and the issue and return dates for that particular student.
for example:
class books_issued{
public: book issued_book;
books_issued *new;
<put your issue data like:>
unsigned int Account_Number; //if issued
int Copies_of_Book;
Date Return_Date;
<and issue related methods:>
};
now declare an object of books_issued class in student class,
and write a method to add, remove and view books that have been issued by that particular student.
--- If you need any help with creating linked lists for this purpose, let me know I will be more than happy to help as I had done a very similar library management program for my project as well :-) ---

Printing derived classes through a function

I'm new to c++ because all throughout our lab activities we just use C# and I'm still trying get the gist of it. So our last lab, which uses c++, is to show our genealogy-our family tree- and my problem right now is printing. Can someone point out what's wrong in my function or why it's not printing and what's the best way to do it?
#include <iostream>
#include <string>
using namespace std;
class parent
{
public:
void setMom(string mom)
{
mother=mom;
}
void setDad(string dad)
{
father=dad;
}
string getDad(void)
{
return father;
}
string getMom(void)
{
return mother;
}
protected:
string mother;
string father;
};
class Member: public parent
{
public:
string name;
Member()
{
}
void setName(string kid)
{
name=kid;
}
void setStatus(string stat)
{
status=stat;
}
void setAge(int num)
{
age=num;
}
string getName()
{
return name;
}
private:
string status;
int age;
};
char addPerson()
{
Member person;
char mom[50];
char dad[50];
char kid[50];
char stat[7];
int num;
cout<<"Name: ";
cin>>kid;
person.setName(kid);
cout<<"Age: ";
cin>>num;
person.setAge(num);
cout<<"Status(Living/Diseased): ";
cin>>stat;
person.setStatus(stat);
cout<<"Father: ";
cin>>dad;
person.setDad(dad);
cout<<"Mother: ";
cin>>mom;
person.setMom(mom);
}
my function for printing.
void showme()
{
Member person;
cout<<person.getMom();
}
//-----------------------------------------------------MAIN--------------------------------------------------
int main()
{
while(1)
{
int choice;
cout<<" 1. Add member"<<"\n";
cout<<" 2. Edit member"<<"\n";
cout<<" 3. Show family tree"<<"\n";
cout<<" 4. Exit"<<"\n";
cin>>choice;
if(choice==1)
{
addPerson();
}
else if(choice==2)
{
}
else if(choice==3)
{
showme();
}
else if(choice==4)
{
break;
}
}
}

c++ printing object and constructor

Hello everyone I had a problem while running the following program in the last loop it only print three 0 without displaying any result I am not sure what is wrong with the program,perhaps it has something to do with the constructor, however when I write my getter method in the second last loop with setter method it display the correct result. any help will be appreciated.
#ifndef EMPLOYEE_H_INCLUDED
#define EMPLOYEE_H_INCLUDED
#include<string>
using namespace std;
class Employee
{
private:
string name;
int idnumber;
string department;
string position;
public:
Employee(string ,int ,string ,string);
void setName(string );
void setDepartment(string);
void setPosition(string);
void setIDNumber(int);
string getName() const
{
return name;
}
string getDepartment() const
{
return department;
}
string getPosition() const
{
return position;
}
int getIDnumber() const
{
return idnumber;
}
};
#include "Employee.h";
#include <iostream>
#include <iomanip>
using namespace std;
Employee::Employee(string name ,int IDNumber ,string department ,string position)
{
name=name;
idnumber=IDNumber;
department=department;
position=position;
}
void Employee::setDepartment(string Department)
{
department=Department;
}
void Employee::setName(string Name)
{
name=Name;
}
void Employee::setPosition(string Position)
{
position=Position;
}
void Employee::setIDNumber(int Number)
{
idnumber=Number;
}
int main()
{
string name;
int IDNumber;
string department;
string position;
const int Item=3;
Employee info1(" ",0," "," ");
Employee info2(" ",0," "," ");
Employee info3(" ",0," "," ");
Employee Info[Item]={info1,info2,info3};
}
for(Employee element:Info)
{
cout<<"please enter your name "<<endl;
cin>>name;
element.setName(name);
cout<<element.getName()<<endl;
cout<<"please enter your department "<<endl;
cin>>department;
element.setDepartment(department);
cout<<element.getDepartment()<<endl;
cout<<"please enter your position"<<endl;
cin>>position;
element.setPosition(position);
cout<<element.getPosition()<<endl;
cout<<"please enter your IDNumber"<<endl;
cin>>IDNumber;
element.setIDNumber(IDNumber);
cout<<element.getIDnumber()<<endl;
}
for(Employee element:Info)
{
cout<<element.getName()<<setw(8)<<element.getDepartment()
cout<<setw(8)<<element.getPosition()<<setw(8)<<element.getIDnumber()<<endl;
}
}
for(Employee element:Info)
element is a copy of an element from Info array. You are busy filling this copy, then it gets discarded. Contents of Info remain unchanged, with empty strings all around.
Make it
for(Employee& element:Info)
Note the ampersand.

Using constructor when passing an array of objects through composition [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
#include <iostream>
#include <cstring>
using namespace std;
class assistantnurse{
private:
char* id;
char dutytime;
public:
assistantnurse(char[] ="", char =' ');
void setid(char*);
void setdutytime(char);
char* getid()const;
char getdutytime()const;
void print()const;
void inputinfo();
~assistantnurse();
};
assistantnurse::~assistantnurse()
{
delete[] id;
id=0;
}
assistantnurse::assistantnurse(char* i, char t)
{
setid(i);
setdutytime(t);
}
void assistantnurse::setid(char *i)
{
int l=strlen(i);
id=new char[l+1];
strncpy(id,i,l);
id[l]='\0';
}
void assistantnurse::setdutytime(char t)
{
dutytime=t;
}
char* assistantnurse::getid()const{return id;}
char assistantnurse::getdutytime()const{return dutytime;}
void assistantnurse::print()const
{
cout<<"ID: "<<id<<endl;
cout<<"Duty time: "<<dutytime<<endl<<endl;
}
void assistantnurse::inputinfo()
{
char nurseid[20];
char time;
cout<<"Enter nurse ID: ";
cin>>nurseid;
do
{
cout<<"Enter nurse Duty time(d for day/n for night): ";
cin>>time;
}while(time!='n' && time!='d');
setid(nurseid);
setdutytime(time);
}
class treatingphysician{
private:
char* id;
char* phonenumber;
assistantnurse assistant[2]; //3 assistant nurses through composition from assistantnurse class
public:
void setid(char*);
void setphonenumber(char*);
char* getid()const;
char* getphonenumber()const;
void setnurse();
void getnurse()const;
treatingphysician(char[] =" ", char[] =" ", assistantnurse[]);
~treatingphysician();
void print()const;
void inputinfo();
};
void treatingphysician::inputinfo()
{
char i[20];
char p[15];
cout<<"Enter physician ID: ";
cin>>i;
setid(i);
cout<<"Enter physican phone number: ";
cin>>p;
setphonenumber(p);
setnurse();
}
void treatingphysician::print()const
{
cout<<"Physician ID: "<<id<<endl;
cout<<"Physician phone number: "<<phonenumber<<endl<<endl;
getnurse();
}
treatingphysician::~treatingphysician()
{
delete[] id;
id=0;
delete[] phonenumber;
phonenumber=0;
}
treatingphysician::treatingphysician(char*i, char*p, assistantnurse k[2]):assistant(k)
{
setid(i);
setphonenumber(p);
}
void treatingphysician::setid(char*i)
{
int l=strlen(i);
id=new char[l+1];
strncpy(id,i,l);
id[l]='\0';
}
void treatingphysician::setphonenumber(char*p)
{
int l=strlen(p);
phonenumber=new char[l+1];
strncpy(phonenumber,p,l);
phonenumber[l]='\0';
}
char* treatingphysician::getid()const{return id;}
char* treatingphysician::getphonenumber()const{return phonenumber;}
void treatingphysician::setnurse()
{
for(int i=0;i<3;i++)
{
cout<<"Enter info for nurse #"<<i+1<<endl;
assistant[i].inputinfo();
cout<<"\n";
}
}
void treatingphysician::getnurse()const
{
for(int i=0;i<3;i++)
{
cout<<"Nurse #"<<i+1<<" ";
assistant[i].print();
}
}
int main()
{
treatingphysician e;
e.inputinfo();
e.print();
system("pause");
return 0;
}
two classes;
A nurse class and a physician class;
The physician has 3 nurses and all his attributes.
Using composition to fix this, but i'm stuck on the array of objects for the 3 nurses.
In my second constructor i faced a problem i used to work on composition before with the same method but the only difference is that it wasn't an array of objects used in composition. now I'm stuck.. Please Help
Short example...
`class A{
public:
A();
};
class B{
private:
A a[2];
public:
B(A[]);
};
B::B(A c[2]):a(c){}`
Use std::vector instead of raw arrays.
class A
{
public:
A();
};
typedef std::vector< A > A_vector;
class B
{
private:
A_vector m_a;
public:
B( A_vector av ) : m_a( av )
{
}
};

3 (related I believe) errors in my c++ program

I am receiving the following errors with my code:
Error 3 error LNK1120: 1 unresolved
externals c:\users\toking\documents\visual studio
2012\Projects\11.9\Debug\11.9.exe 11.9
Error 2 error LNK2001: unresolved external symbol "public: double
__thiscall Package::calculateCost(float,double)" (?calculateCost#Package##QAENMN#Z) c:\Users\toking\documents\visual
studio 2012\Projects\11.9\11.9\11.9_main.obj 11.9
Error 1 error LNK2019: unresolved external symbol "public: double
__thiscall Package::calculateCost(float,double)" (?calculateCost#Package##QAENMN#Z) referenced in function "public:
double __thiscall
overnightPackage::calcCostOvernight(float,double,double)"
(?calcCostOvernight#overnightPackage##QAENMNN#Z) c:\Users\toking\documents\visual
studio 2012\Projects\11.9\11.9\11.9.obj 11.9
This is my second semester of C++ and my first time using inheritance. I tried calling the functions similarly to how my book calls them, but all of the errors seem to be related to my derived classes functions. Any help is greatly appreciated.
// Main
#include <iostream>
#include "stdafx.h"
#include <string>
using namespace std;
int main()
{
int i;
string customerName,customerAddress,city,state,senderAddress,recipientAddress;
float packageWeight;
string customerCity;
double costPerOunce;
double flatFee;
double additionalCost;
string customerState;
int customerZipcode;
Package base;
twoDayPackage twoday;
overnightPackage overnight;
cout<<" *****Welcome To The American Package Delievery Services*****"<<endl<<endl;
cout<<"Please Fill In The Requested Data Follow: "<<endl<<"-----------------------------------------"<<endl<<endl;;
cout<<"Enter Customer Name "<<endl<<endl;
cin>>customerName;
cout<<endl;
base.setName(customerName);
cout<<"Enter Customer Address"<<endl<<endl;
cin>>customerAddress;
cout<<endl;
base.setAddress(customerAddress);
cout<<"Enter Customer City"<<endl<<endl;
cin>>customerCity;
cout<<endl;
base.setCity(customerCity);
cout<<"Enter Customer State"<<endl<<endl;
cin>>customerState;
cout<<endl;
base.setState(customerState);
cout<<"Enter Customer ZIP code"<<endl<<endl;
cin>>customerZipcode;
cout<<endl;
base.setZip(customerZipcode);
cout<<"Enter Weight"<<endl;
cin>>packageWeight;
cout<<endl;
cout<<"Enter Cost Per Ounce"<<endl;
cin>>costPerOunce;
cout<<endl;
cout<<"Please Enter Your Choice From The Menu Below:"<<endl<<endl;
cout<<" 1- Calculate Base Cost "<<endl<<endl;
cout<<" 2- Calculate Two Day Cost "<<endl<<endl;
cout<<" 3- Calculate Over Night Cost"<<endl<<endl;
cin>>i;
cout<<endl;
switch (i)
{
case 1 :
base.calculateCost(packageWeight,costPerOunce);
break;
case 2 :
cout<<"Enter Flat Cost"<<endl<<endl;
cin >> flatFee;
twoday.calcShippingCost(packageWeight,costPerOunce,flatFee);
break;
case 3 :
cout<<"Enter The Additional Cost"<<endl<<endl;
cin >> additionalCost;
overnight.calcCostOvernight(packageWeight,costPerOunce,additionalCost);
break;
default:
cout << "INVALID CHOICE....Please Enter ur Choice Number From 1-->3 "<<endl;
}
cout<<"Enter sender address "<<endl<<endl;
cin>>senderAddress;
cout<<endl;
base.setSender( senderAddress);
cout<<"Enter ricipent address"<<endl<<endl;
cin>>recipientAddress;
cout<<endl;
base.setRecipient(recipientAddress);
cout<<"address from:"<< senderAddress<<endl;
cout<<"To:"<<recipientAddress<<endl;
system ("pause");
return 0;
}
// Header
#include <iostream>
#include <string>
using namespace std;
class Package // Base Class
{
private:
string name, city, state, sender, recipient;
int zip;
string address;
float weight;
double cost;
public:
void setName(string);
string getName();
void setCity(string);
string getCity();
void setState(string);
string getState();
void setZip(int);
int getZip();
void setAddress(string);
string getAddress();
void setSender(string);
string getSender();
void setRecipient(string);
string getRecipient();
double calculateCost(float , double);
};
class twoDayPackage: public Package
{
public:
double calcShippingCost(float, double, double);
private:
double flatFee;
};
class overnightPackage: public Package
{
public:
double calcCostOvernight(float, double, double);
private:
double overnightCost;
};
// Cpp
#include <iostream>
#include "stdafx.h"
#include <string>
using namespace std;
void Package::setName(string n)
{
name = n;
}
void Package::setCity(string c)
{
city = c;
}
void Package::setState(string s)
{
state = s;
}
void Package::setZip (int zp)
{
zip = zp;
}
void Package::setAddress(string adr)
{
address = adr;
}
void Package::setSender(string sen)
{
sender = sen;
}
void Package::setRecipient(string rec)
{
recipient = rec;
}
string Package::getName()
{
return name;
}
string Package::getCity()
{
return city;
}
string Package::getState()
{
return state;
}
int Package::getZip()
{
return zip;
}
string Package::getAddress()
{
return address;
}
string Package::getSender()
{
return sender;
}
string Package::getRecipient()
{
return recipient;
}
double calculateCost(float weight,double costPerOunce)
{
double z;
z = weight * costPerOunce;
cout<< "The Base Cost = " <<z << endl<<endl;
return z;
}
double twoDayPackage::calcShippingCost(float weight, double costPerOunce, double flatFee)
{
double z;
z = calculateCost(weight,costPerOunce) + flatFee;
cout << "The TwoDayPackage Cost = " << z << endl;
return z;
}
double overnightPackage::calcCostOvernight(float weight,double costPerOunce,double additionalCost )
{
double z;
z = calculateCost(weight, costPerOunce)+(additionalCost * weight);
cout<< "The OvernightPackage Cost = " <<z << endl;
return z;
}
Your code can't decide whether calculateCost is a member function of Package or not. Pick one way and stick to it.