needed help in solving of inheritance problem in c++ problem - c++

I have a C++ question regarding inheritance:
Write a program that has a class Train with data members seats_first_class, seats_second_class and seats_ac_2tier and member functions to set and display data.
Derive a class Reservation that has data members booked_first_class, booked_second_class and booked_ac_2tier and functions to book and cancel tickets and display status.
This is the code which I have written
https://onlinegdb.com/CTAdFBM_C
#include <iostream>
using namespace std;
class Train
{
protected :
int seats_first_class,seats_second_class,seats_ac_2tier;
public :
//Train(int a,int b,int c) : seats_first_class(a),seats_second_class(b),seats_ac_2tier(c){}
//Train() : seats_first_class(0),seats_second_class(0),seats_ac_2tier(0){}
void Read()
{
cout<<"Enter the seats in 1st class..."; cin>>seats_first_class; cout<<endl;
cout<<"Enter the seats in 2nd class..."; cin>>seats_second_class; cout<<endl;
cout<<"Enter the seats in AC 2 tier..."; cin>>seats_ac_2tier; cout<<endl;
}
void Disp()
{
cout<<"The number of seats in booked by you in 1st class are "<<seats_first_class<<endl;
cout<<"The number of seats in booked by you in 2nd class are "<<seats_second_class<<endl;
cout<<"The number of seats in booked by you in AC 2 tier class are "<<seats_ac_2tier<<endl;
}
};
class Reservation : public Train
{
protected :
bool booked_first_class=0,booked_second_class=0,booked_ac_2tier=0;
public :
bool evaluvate_seat()
{
char ch;
cout<<"Have you booked tickets for 1st class\n"; cin>>ch;
if(ch='y')
return booked_first_class=true;
cout<<"Have you booked tickets for 2nd class\n"; cin>>ch;
if(ch='y')
return booked_second_class=true;
cout<<"Have you booked tickets for AC 2 tier\n"; cin>>ch;
if(ch='y')
return booked_ac_2tier=true;
}
void status_disp(bool a)
{
if(a==true)
cout<<"Status : Reserved"<<endl;
else
cout<<"Status : Not Reserved"<<endl;
}
void cancel_seat()
{
int n;
cout<<"Enter the the type of compartment you want to delete\n"; cin>>n;
switch(n)
{
case 1:
cout<<"Cancelling your ticket in 1st class"<<endl;
booked_first_class=false;
break;
case 2:
cout<<"Cancelling your ticket in 2nd class"<<endl;
booked_second_class=false;
break;
case 3:
cout<<"Cancelling your ticket in AC 2 tier class"<<endl;
booked_ac_2tier=false;
break;
}
}
};
int main()
{
Reservation r1,r2;//Train t1,t2;
r1.Read(); r2.Read();
r1.Disp(); r2.Disp();
r1.evaluvate_seat(); r1.status_disp(Resevation :: evaluvate_seat());
r2.cancel_seat(); r2.status_disp(Resevation :: evaluvate_seat());
return 0;
}
These are the errors i am facing:
main.cpp:76:38: error: ‘Resevation’ has not been declared
r1.evaluvate_seat(); r1.status_disp(Resevation :: evaluvate_seat());
^~~~~~~~~~
main.cpp:77:35: error: ‘Resevation’ has not been declared
r2.cancel_seat(); r2.status_disp(Resevation :: evaluvate_seat());
Edit :
I tried to rectify some mistakes but still am not successful in making the code error free. Anyone's help is really appreciable

Related

how to create a c++ function which create new object every time user input new data?

i'm new in c++ and i've been given an assignment where to create a program that can entry Employee Name, Work Hour, Overtime Duration.
There is a menu Insert Employee, View Employee where option view will display all list employee that already input before: Name, Work Hour, Overtime, Salary.
User limited to input max 5 Employee, so if user input 5 different employee, View Employee option will display all that 5.
I have already create a class Employee and i'm still confused how my inputEmp function to be able create new object every time user choose Insert Employee option?
Here's a brief of my code
class Employee {
string name;
int salaryTot, hour, overtime;
public:
Employee(){hour = 8;}
void calcSalary(){
int salary = hour * 20000;
salaryTot = overtime * 30000 + salary;
};
void putName(string name){name = name};
void putOvertime(int overtime){overtime = overtime};
void getName(){return name;};
void getHour(){return hour;};
void getOvertime(){return overtime;};
void getSalary(){return salaryTot;};
}
void inputEmp(){
//<blabla_code> this line should create new object for this new user input
cout<<string(12, '\n')<<"Input Employee's name [5-25] : ";
cin>>//blabla.putName()
cout<<"\nInput overtime's duration [0 - 4] : ";
cin>>//blabla.putOvertime();
//blabla.calcSalary
cout<<"Success insert new employee!"<<endl;
}
void printALL(){
cout<<"Name :"<<\\blabla.getName()
cout<<"Work Hour :"<<\\blabla.getHour()
cout<<"Overtime :"<<\\blabla.getOvertime()
cout<<"Salary :"<<\\blabla.getSalary()
}
int Layout(){
int choice = 0;
while(choice !=3){
do{
cout<<string(12, '\n')<<"adeqecil COMPANY"<<endl;
cout<<string(16, '=')<<endl;
cout<<"1.Insert Employee"<<endl;
cout<<"2.View Employee"<<endl;
cout<<"3.Exit"<<endl;
cout<<"Input choice : "; cin>>choice;
} while(choice <1 && choice >5);
switch (choice)
{
case 1: inputEmp();
break;
case 2: printAll();
break;
case 3: return 0;
break;
}
}
return 0;
}
This statement is confusing : while(choice <1 && choice >5);
make a new object for every new employee and pass it into PrintALL function.
void printALL(emp_object_1){
cout<<"Name :"emp_object_1.getName();
cout<<"Work Hour :"emp_object_1.getHour();
cout<<"Overtime :"emp_object_1.getOvertime();
cout<<"Salary :"emp_object_1.getSalary(); }
Your code has so many errors. However, hope this helps.

structure name not declared and swap function swaps without pass by refrence

I have this code,which takes input of 3 students from division a and b each.
those 2 divions are sorted and merged in a 3rd array according to birth dates of students.
the swap function ,I have not passed anything by refrence still its swapping and sort output is correct !!!.
NOTE:the line below #include..
void swap(struct a,struct b)
it should be
void swap(struct student a,struct student b)
but without changing that the program is runnong and giving correct outputs !! how ??
#include<iostream>
#include<string.h>
using namespace std;
void swap(struct a,struct b);
void findweek(struct student ar[10],int l,int bd1,int bm1);
struct student
{
//m is for month and b is for birthdate prn=prn number,name=name of student
int m,bd;
char prn[10],name[10];
};
int main()
{
//2 divisions a and b declared and will be merged into c
struct student a[3], b[3], c[6];
//division a input
for(int i=0;i<3;i++)
{
cout<<" Enter name of student "<<endl;
cin>>a[i].name;
cout<<"Enter prn no. "<<endl;
cin>>a[i].prn;
cout<<"Enter birth day "<<endl;
cin>>a[i].bd;
cout<<"Enter birth month "<<endl;
cin>>a[i].m;
}
//sorting of a
for(int i=0;i<3;i++)
{
for(int j=i;j<3;j++)
{
if(a[i].m>a[j].m)
{
swap(a[i],a[j]);
}
else if(a[i].m==a[j].m)
{
if(a[i].bd>a[j].bd)
{
swap(a[i],a[j]);
}
}
}
}
//division b input
for(int i=0;i<3;i++)
{
cout<<" Enter name of student "<<endl;
cin>>b[i].name;
cout<<"Enter prn no. "<<endl;
cin>>b[i].prn;
cout<<"Enter birth day "<<endl;
cin>>b[i].bd;
cout<<"Enter birth month "<<endl;
cin>>b[i].m;
}
//sorting of b
for(int i=0;i<3;i++)
{
for(int j=i;j<3;j++)
{
if(b[i].m>b[j].m)
{
swap(b[i],b[j]);
}
else if(b[i].m==b[j].m)
{
if(b[i].bd>b[j].bd)
{
swap(b[i],b[j]);
}
}
}
}
cout<<"-----------------------"<<endl;
cout<<"Division A"<<endl;
int count=0;
for(int i=0;i<3;i++)
{ //c has merged array , a being filled first
c[i]=a[i];
count++;
cout <<c[i].name<<"\t"<<c[i].prn<<"\t"<<c[i].bd<<"|"<<c[i].m<<endl;
}
cout<<"Division B"<<endl;
for(int i=0;i<3;i++)
{
//resume filling the array from count
c[count]=b[i];
cout <<c[count].name<<"\t"<<c[count].prn<<"\t"<<c[count].bd<<"|"<<c[count].m<<endl;
count++;
}
int bd1,bm1;
cout<<"Enter date to find birthdays in that week "<<endl;
cin>>bd1;
cout<<"Enter corresponding month "<<endl;
cin>>bm1;
findweek(c,count,bd1,bm1);
return 0;
}
//to swap the structure student arrays for sorting
void swap(struct student a,struct student b)
{
struct student t;
t=a;
a=b;
b=t;
}
void findweek(struct student ar[10],int l,int bd1,int bm1)
{
int count=0;
for(int i=0;i<l;i++)
{
int month_end=30;
int next_month=bm1+1;
//if(bd1>=23)
int end_date=bd1+7-month_end;
// else
int endofweek=bd1+7;
//l is length of ar , ar=copy of merged array, bd1&bm1 are date and month to search for birthday in that week
if((ar[i].m==bm1&&ar[i].bd>=bd1&&ar[i].bd<=endofweek)||ar[i].m==bm1+1&&ar[i].bd<=end_date)
{
if(month_end-bd1>7)
cout <<ar[i].name<<"\t"<<ar[i].prn<<"\t"<<ar[i].bd<<"|"<<ar[i].m<<endl;
else
{
if((ar[i].m==bm1&&ar[i].bd>=bd1)||(ar[i].m==next_month&&ar[i].bd<=end_date))
{
cout <<ar[i].name<<"\t"<<ar[i].prn<<"\t"<<ar[i].bd<<"|"<<ar[i].m<<endl;
}
}
count++;
if(count>7)
break;
}
}
}
It compiles even if you remove this line:
void swap(struct a,struct b);
Also, it compiles if you remove the whole swap function.
How is it?
Quite simple.
You are defining a function that takes two arguments: an incomplete type struct a and an incomplete type struct b.
That function is simply discarded from the overload set while searching the one to be used at function call.
Your main is not using your swap function. Instead, it's using the one from std:: namespace.
It is probably introduced by iostream or string, it's implementation defined.
Try changing the name of the function or putting a throw in your implementation of swap. In the second case, your runtime won't be affected.
Minimal, (not-)working example to reproduce the issue:
void f(struct s);
struct S {};
int main() { f(S{}); }
void f(S) {}
As you can see, the error is that you are referring to an incomplete type struct s.
swap is kinda of a somehow misleading example that compiles for the reasons above.
Reproducing an issue with a minimal example is often helpful.

Declaration terminated Incorrectly error in following code in cpp

Iam trying to develop C++ program for observer pattern but i am getting these errors.
Here's my CPP code , and i getting error continuously : "Declaration termination incorrectly" !
Thanks in Advance
Help me please i am desperate.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Subject{
public : virtual ~Subject();
virtual float attach()=0;
virtual int notify()=0;
};
class Observer{
public : virtual ~Observer();
virtual void update(int type, float amount, float bal)=0;
};
class Account : public Subject
{
public: float attach()
{
char name[12];
int account_no;
float bal;
cout<<"Enter the Name of Account Holder : ";
cin>>name;
cout<<"Enter the Account No. : ";
cin>>account_no;
cout<<"Enter the Balance of his account : ";
cin>>bal;
cout<<"The Name of Account Holder : "<<name;
cout<<"The Account No. : "<<account_no;
cout<<"The Balance of his account : "<<bal;
return bal;
}
int notify()
{
int type;
cout<<"\nMenu :\n\n1) Deposit\n2)Withdrawl\n";
cout<<"Enter the type for transition : \n";
cin>>type;
return type;
}
public: void update(int type, float amount, float bal)
{
char name[12];
int account_no;
if(type==1)
bal=bal+amount;
else if(type==2)
bal=bal-amount;
else
cout<<"Oops! Transition Type is invalild....";
cout<<"\nThe Details of Account Holder after Transition :-\n";
cout<<"The Name of Account Holder : "<<name;
cout<<"The Account No. : "<<account_no;
cout<<"The Balance of his account : "<<bal;
}
};
class obpt{
public : static void main()
{
Account ac;
//AccountUpdate au;
float balance, amt;
int type;
clrscr();
cout<<"\nWelcome To The Program of Observer Pattern of Account Transition\n";
cout<<"\nEnter the Details of Account Holder :-\n";
balance = ac.attach();
cout<<"\nCall notification for Deposit or Withdrawl Transition\n";
type=ac.notify();
cout<<"\nEnter the amount for transition : \n";
cin>>amt;
cout<<"\nAfter The transition the Main balance : \n";
ac.update(type, amt, balance);
getch();
}
}
You are missing a ; at the end of the class declaration. Correct:
class Foo
{
/*...*/
};
In C++, main should be a free function, the obpt class is wrong.
int main()
{
/* ... */
}
The error message you are receiving is coming from the compiler. It is stating that a declaration statement has not been ended correctly.
Somewhere in your code you are missing a semi-colon ; perhaps at the end of a class declaration or after a variable is defined.
Without seeing your code, there is no way for me to pinpoint the problem, but the error message should take you to the line if you double click it!
Regards, Rakesh.

Not understanding the error message I'm getting

I doing a freind function program according to this book I have and I did a little of my own code to the program. I puzzle because I get this error message that the "room_num" is undeclared and intellisense identifier "room_num" is undefine. I need help in understanding why this is happen and how to fix it. Here is the code I have been working on for the passed three weeks.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
class HotelRoom
{
friend int Transfer( HotelRoom&, int);
private:
int room_num;
int transroom_num;
int room_cap;
int occup_stat;
double daily_rt;
public:
HotelRoom(int room, int roomcap, int occup, int transroom, double rate = 89.00);
~HotelRoom();
int Display_Number(); //Displays room number and add the method Display_Guest()
int Get_Capacity();
int Get_Status();
double Get_Rate();
int Change_Status(int);
double Change_Rate(double);
void Display_Guest();
};
HotelRoom::~HotelRoom()
{
cout << endl<<endl;
cout << "Guest in room "<<room_num << " has checked out." <<endl;
}
int HotelRoom::Display_Number()
{
return room_num;
}
int HotelRoom::Get_Capacity()
{
return room_cap;
}
int HotelRoom::Get_Status()
{
return occup_stat;
}
int HotelRoom::Change_Status(int occup)
{
occup_stat = occup;
if (occup > room_cap)
{
return -1;
}
else
return occup_stat;
}
double HotelRoom::Get_Rate()
{
return daily_rt;
}
double HotelRoom::Change_Rate(double rate)
{
daily_rt = rate;
return daily_rt;
}
int Transfer(HotelRoom& room_r1, int transroom)
{
//if guest transfers to different hotel room, room is vacant and transroom is now occupied
room_r1.room_num = room_r1.transroom_num;
return room_num;
}
int main()
{
cout<< setprecision(2)
<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);
int room = 0;
int roomcap = 4;
int transroom;
int occup;
double rate = 89.00;
cout<<"\nEnter the room number: "<<endl;
cin>>room;
cout<<"\nEnter the amount of guest to occupy this room: "<<endl;
cin>>occup;
cout<<"\nThe guest has decided to transfer rooms"<<endl;
cout<<"\nEnter the room to transfer the guest to"<<endl;
cin>>transroom;
HotelRoom room1(room,roomcap, occup, transroom, rate ); //initialize the object
if (room1.Change_Status(occup) == -1)
{
cout<<"You have exceeded the room capacity"<<endl;
}
else
{
cout <<"\nThe room number is ";
room1.Display_Number();
cout<<"."<<endl;
cout<<"\nThe name of the primary guest is ";
room1.Display_Guest();
cout <<"."<<endl;
cout<<"\nThe number of guest in the room is "<<room1.Change_Status(occup)<<"." <<endl;
cout<<"\nThe daily rate for room "<<room<< " is "<<room1.Get_Rate()<<"."<<endl<<endl;
cout<<"\nYou have tranferred the guest from room"<<room1.Display_Number()<<"to" <<Transfer(room1,transroom)<<endl;
}
cout<<"\nRoom ";
room1.Display_Number();
cout<<" is vacant."<<endl;
system("PAUSE");
return 0;
}
The function Transfer is not a method of HotelRoom, still you are trying to access room_num in it as if it was. You need to specify which room_num of which HotelRoom instance you mean. Probably you meant return room_r1.room_num instead of return room_num.
Also in your Transfer function you never use the parameter transroom, instead you are using a transroom_num from room_r1. This is probably not what you want.
Finally you haven't implemented the constructor and DisplayRoom of HotelRoom. You should create a stubs, which do nothing or print warnings as long as you haven't implemented the methods properly, so you can at least compile and link the code.
Since you are a beginner I would just stick with member functions and class private variables until you get better at it.
As far as the error message, my guess is that inside the function you are using room_num does not have access to the private parts of the HotelRoom class. Notice I said guess, that's because you should copy and paste the text on the output window here so we can see what exactly is happening.
First, you have to identify that room_num is class member variable.
int Transfer(HotelRoom& room_r1, int transroom)
{
room_r1.room_num = room_r1.transroom_num;
//because room_num is not non class member variable, you have to write like below.
return room_r1.room_num;
//return room_num;
}
Secondly, you did not write definition HotelRoom::HotelRoom(int,int,int,int,double), HotelRoom::Display_Guest(void). So you have to write this constructor and function for avoiding error LNK2019.

Cannot create pointers of an abstract class in function main()

#include<iostream.h>
#include<conio.h>
#include<string.h>
class flight
{
private :
int flightno;
char source[30],destination[30];
protected :
double fare;
public :
flight()
{
flightno=0;
source[0]='\0';
destination[0]='\0';
fare=0.0 ;
}
flight(int f,char s[],char d[],double fr)
{
flightno=f;
strcpy(source,s);
strcpy(destination,d);
fare=fr;
}
flight(flight &f)
{
flightno=f.flightno;
strcpy(source,f.source);
strcpy(destination,f.destination);
fare=f.fare;
}
virtual void accept()
{
cout<<"\nEnter Fligt no :";
cin>>flightno;
cout<<"\nEnter source :";
cin.getline(source,30);
cout<<"\nEnter destination :";
cin.getline(destination,30);
cout<<"\nEnter fare :";
cin>>fare;
}
virtual void display()
{
cout<<"\nFlight number :"<<flightno;
cout<<"\nsource :"<<source;
cout<<"\nDestination :"<<destination;
cout<<"\nflight fare :"<<fare;
}
virtual double computefare()=0;
};
class domestic : public flight
{
private :
int noc,noa;
double totalfare,discount;
public :
domestic()
{
noc=0;
noa=0;
totalfare=0.0;
discount=0.0;
}
domestic(int f,char s[],char d[],double fr,int nc,int na):flight(f,s,d,fr)
{
noc=nc;
noa=na;
totalfare=0.0;
discount=0.0;
}
domestic(domestic &d):flight(d)
{
noc=d.noc;
noa=d.noa;
totalfare=d.totalfare;
discount=d.discount;
}
void accept()
{
flight::accept();
cout<<"\nEnter no. of adults :";
cin>>noa;
cout<<"\nEnter no. of children :";
cin>>noc;
}
void display()
{
flight::display();
cout<<"\n no. of adults :"<<noa;
cout<<"\n no. of children :"<<noc;
cout<<"\n total fare :"<<totalfare;
cout<<"\n discount :"<<discount;
if(discount!=0)
cout<<"After disc: "<<(totalfare-discount);
}
double computefare()
{
totalfare=noc*flight::fare*0.5+noa*flight::fare;
if(totalfare>40000)
discount=0.02*totalfare;
return totalfare-discount;
}
};
class internatinal : public flight
{
private :
int nop;
double totalfare,tax;
public :
internatinal()
{
nop=0;
totalfare=0.0;
tax=0.0;
}
internatinal(int f,char s[],char d[],double fr,int np):flight(f,s,d,fr)
{
nop=np;
totalfare=0.0;
tax=0.0;
}
internatinal(internatinal &i):flight(i)
{
nop=i.nop;
totalfare=i.totalfare;
tax=i.tax;
}
void accept()
{
flight::accept();
cout<<"\nEnter no. of passenger :";
cin>>nop;
}
void display()
{
flight::display();
cout<<"\n no. of passenger :"<<nop;
cout<<"\n total fare :"<<totalfare;
cout<<"\n tax :"<<tax;
cout<<"After tax: "<<(totalfare+tax);
}
double computefare()
{
totalfare=nop*flight::fare;
tax=0.30*totalfare;
return totalfare+tax;
}
};
int main()
{
int i,n,ch;
double total=0;
cout<<"\n Enter no of transaction :" ;
cin>>n;
flight *f=new flight[n];
for(i=0;i<n;i++)
{
cout<<"\n Enter 1 : domestic \n 2. internatinal :";
cin>>ch;
f[i]=ch==1?new domestic():new internatinal();
f[i]->accept();
total+=f[i]->computefare();
}
cout<<"\n totaL fare :"<<total;
getch();
return 0;
}
I am using turdo C++ complier . My code is not able to compile the error lies in the main() in "flight *f=new flight[n];" line (error is :1.)Cannot create instance of abstract
class 'flight' in function main()
2.) Class 'flight' is abstract because of 'flight::computefare() = 0' in function main() ).
As far as i can remember we cannot create objects of an abstract class but we can create pointers of it. And here i am creating pointers only but still m getting this error.
You're creating a pointer-to-flight, but you're also trying to create n flight objects: the array you're creating is an array-of-flight, not an array-of-pointer-to-flight.
So this cannot work.
You could use for instance a std::vector<flight*> (possibly using a smart pointer inside the container too). (Or an array of pointer-to-flight.)
For your assignment, don't try and scrunch it all up in a single line, it buys you nothing. Write it more clearly and it will work:
if (ch == 1)
f[i] = new domestic();
else
f[i] = new internatinal();
(The other option is to use a cast, but that will make that line even less readable.)