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 )
{
}
};
Related
I have written a code like bill payment. Code is working fine but there are many warnings in my code which I want to remove. One of the most frequent warning is deprecated conversion from string constant to 'char* . I have tried many things and some of the warnings are gone but not all. Please anybody point out at my mistakes??
P.S: I have already tried replacing char* to const char* , but then I am not able to exchange or swap values and it was causing error. Any other solution??
Below is the code
#include<iostream>
#include<string.h>
using namespace std;
class item
{
private:
int barcode;
char* item_name;
public:
item (int num=0, char* name="NULL") : barcode(num)
{
item_name=new char[strlen(name)+1];
strcpy(item_name,name);
}
void setbarcode(int num)
{
barcode=num;
}
int getbarcode()
{
return barcode;
}
void scanner()
{
int num;
cin>>num;
setbarcode(num);
}
void printer()
{
cout <<"\nBarcode"<<"\t\t"<<"Item Name"<<"\t\t"<<"Price"<<endl;
cout <<barcode<<"\t\t"<<item_name<<"\t\t\t";
}
~item()
{
delete[]item_name;
}
};
class packedfood : public item
{
private :
int price_per_piece;
public :
packedfood(int a=0, int num=0, char* name = "NULL") : price_per_piece(a),item(num,name)
{
}
void setprice(int num)
{
price_per_piece=num;
}
int getprice()
{
return price_per_piece;
}
void scanner()
{
item::scanner();
}
void printer()
{
item::printer();
cout<<getprice()<<" Per Piece";
}
~packedfood()
{
}
};
class freshfood: public item
{
private:
int price_per_rupee;
float weight;
public:
freshfood(float b=0, int a=0,int num=0,char* name="NULL") : weight(b),price_per_rupee(a),item(num,name)
{
}
void setweight(float b)
{
weight=b;
}
int getweight()
{
return weight*50;
}
void printer()
{
item::printer();
cout<<getweight()<<" Per Rupee"<<endl<<endl;
}
void scanner()
{
item::scanner();
}
~freshfood()
{
}
};
int main()
{
item x(389,"Anything");
item y;
packedfood m(10,118,"Chocolate");
packedfood n;
freshfood r(20.9,93,357,"Fruits");
freshfood s;
cout <<"\n\n Enter the Barcode for Packed food : ";
m.scanner();
m.printer();
cout <<"\n\n Enter the Barcode for Fresh food : ";
r.scanner();
r.printer();
system("PAUSE");
return 0;
}
It has two issues
1). program is not taking input it exits without getting value of variable option.
2).Also my base and derived class are not initializing they are displaying garbage value.
Here is complete code.
#include<iostream>
using namespace std;
class BeverageItem
{
protected:
string name;
double price;
public:
void set_name(string n);
string get_name();
void set_price(double pr);
double get_price();
};
void BeverageItem::set_name(string n)
{
name=n;
}
string BeverageItem::get_name()
{
return(name);
}
void BeverageItem::set_price(double pr)
{
price=pr;
}
double BeverageItem::get_price()
{
return(price);
}
class HotBeverage:public BeverageItem
{
private:
int tea_bags;
int whiteners;
public:
//HotBeverage(int bg,int wht);
void set_teabags(int t_bags);
int get_teabags();
int getwhiteners();
void set_whiteners(int wht);
double basePrice();
double computeTax();
double totalCost();
void print();
};
double HotBeverage::computeTax()
{
return (0.16*price);
}
double HotBeverage::totalCost()
{
return(price+computeTax());
}
double HotBeverage::basePrice()
{
double pr;
if((tea_bags==1)&& (whiteners==1))
{
pr=20;
}
else if((tea_bags>1)&& (whiteners>1))
{
tea_bags=tea_bags-1;
pr=20+(5*tea_bags);
}
set_price(pr);
return(pr);
}
void HotBeverage::set_teabags(int t_bags)
{
tea_bags=t_bags;
}
int HotBeverage::get_teabags()
{
return(tea_bags);
}
int HotBeverage::HotBeverage::getwhiteners()
{
return(whiteners);
}
void HotBeverage::set_whiteners(int wht)
{
whiteners=wht;
}
void HotBeverage::print()
{
cout<<"Name: "<<name<<endl;
cout<<"Tax:"<<computeTax()<<endl;
cout<<"Total Cost: "<<totalCost()<<endl;
}
class ColdBeverage:public BeverageItem
{
private:
int drinkSize;
public:
//ColdBeverage(int drinkSize);
void setDrinkSsize(int sz);
int getDrinkSize();
double basePrice();
double computeTax();
double totalCost();
void print();
};
void ColdBeverage::print()
{
cout<<"Name: "<<name<<endl;
cout<<"Tax:"<<computeTax()<<endl;
cout<<"Total Cost: "<<totalCost()<<endl;
}
void ColdBeverage::setDrinkSsize(int sz)
{
drinkSize=sz;
}
int ColdBeverage::getDrinkSize()
{
return(drinkSize);
}
double ColdBeverage::computeTax()
{
return (0.16*price);
}
double ColdBeverage::totalCost()
{
return(price+computeTax());
}
double ColdBeverage::basePrice()
{
double pr;
double regularPr=30;
switch(drinkSize)
{
case 1: //regular,
pr=regularPr;
break;
case 2: //large.
price=1.5*regularPr;
break;
case 3: //extra large.
price=2*regularPr;
break;
}
set_price(pr);
return(pr);
}
int main()
{
string name;
int option;
cout<<"Enter The Beverage Name=";
cin>>name;
cout<<"1. For Hot Beverage\n\n";
cout<<"2. For Cold Beverage\n\n";
cout<<"Select Your Choice(1,2)=";
cin>>option;
BeverageItem bi;
bi.set_name(name);
**//some other code here.**
return 0;
}
1) I don't really see a problem with the compiler i tried with
http://cpp.sh/4p5mw
2) POD's in member variables are not default initialised.
https://stackoverflow.com/a/15212447/4669663
Here was the issue
cin>>name;
cin stops when whitespaces are entered in a string so I would have to use
getline(cin,name);
Second issue was because i was not calling the basePrice() before print()
thanks to everyone
I'm getting into polysorphism. I'm in trouble with vector when I call function. This is my code:
Class Customer:
#pragma once
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Customer
{
protected:
string id;
float money;
public:
Customer();
~Customer();
virtual void Input();
virtual void Output();
string GetId()
{
return id;
}
void SetId(string ID)
{
id = ID;
}
};
Class LoyalCustomer:
#pragma once
#include"Customer.h"
class LoyalCustomer:public Customer
{
int level; //level of relationship
public:
LoyalCustomer();
~LoyalCustomer();
void Input();
void Output();
};
RegularCustomer:
#pragma once
class RegularCustomer:public Customer
{
public:
RegularCustomer();
~RegularCustomer();
void Input();
void Output();
};
Class SuperMarket:
#pragma once
#include"LoyalCustomer.h"
#include"RegularCustomer.h"
class SuperMarket
{
vector<Customer*> list;
public:
SuperMarket();
~SuperMarket();
void FindCustomer()
{
string ID;
cout << "Input id of customer: ";
cin >> ID;
for (int i = 0; i<list.size(); i++)
if (ID == list[i]->GetId())
{
//do something
}
}
void Input()
{
string ID;
cout << "Input id of customer: ";
cin >> ID;
Customer *p = NULL;
if (ID[0] == 'L')
{
p = new LoyalCustomer;
p->Input();
p->SetId(ID);
list.push_back(p);
}
if (ID[0] == 'R')
{
p = new RegularCustomer;
p->Input();
p->SetId(ID);
list.push_back(p);
}
}
void Output()
{
//printf customer
}
};
When I call GetID() function in FindCustomer() function in line:if (ID == list[i]->GetId()) and then I run my code, program doesn't notify error but I input "ID" to find, it doesn't find out. I don't know how to fix it. Please help me. Thanks!
Try compare string like ID.compare(list[i]->GetId()) == 0
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;
}
}
}
I want to use the C++ preprocessor to be able to write the following in any C++ block.
class Student {
private:
int age;
int grade;
int courses;
}
int main(){
CREATE_STUDENT 15+62+2 ;
}
The previous code will create a Student with these 3 members.
I want to use + operator overloading.
Any idea of how to do it?
I want EXACTLY the syntax I mentioned above.
Why not just use a constructor:
class Student {
private:
int age;
int grade;
int courses;
public:
Student(int a, int g, int c)
{
age = a;
grade = g;
courses = c;
}
}
int main(){
Student s(15,62,2);
}
Well, I completely fail to understand why you would want to do such a thing. But it is possible, sorta.
You'll need to make it a bit more complex than that to be able to use more than one such "construct" in the same block though.
#include <iostream>
#define GRADE_STUDENT Student student = (Student)
class Student {
public:
Student(int a): age(a), grade(-1), courses(-1), setup(0) {};
Student& operator+(int p)
{
switch(setup) {
case 0: grade = p; break;
case 1: courses = p; break;
default: /* die */ char *p=0; *p=0;
}
setup++;
return *this;
};
void print()
{
std::cout << age << ", " << grade << ", " << courses << std::endl;
};
private:
int age;
int grade;
int courses;
int setup;
};
int main()
{
{
GRADE_STUDENT 15+62+2 ;
student.print();
}
{
GRADE_STUDENT 15+62 ;
student.print();
}
{
GRADE_STUDENT 15+62+2+3 ; // crash
}
return 42;
}
You should template your class instead of working with the preprocessor.