Call function in vector of 2 class inheritance - c++

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

Related

Array of pointers to an object in C++

I am trying to write a super basic program which creates an array of objects under class Receipt. The class includes an int price, string good (name), and a simple function that adds an item to the list. I am stuck because every time I compile it seg faults before it even gets to the add function, meaning something is wrong with my default constructor.
I am still really new to C++ and pointers are probably my biggest struggle. I have looked online and at my lecture notes trying to figure out what I am doing wrong. I feel like it's something small but I cannot figure it out.
Here is my program:
#include <iostream>
#include <string>
using namespace std;
class Receipt {
private:
int price;
string good;
Receipt* goods[500]; //partially filled array
public:
Receipt();
void add(string name, int cost);
string getName();
int getPrice();
void setName(string name_in);
void setPrice(int price_in);
void displayList();
};
Receipt::Receipt()
{
for (int i=0; i < 500; i++)
{
goods[i]->setName("Empty");
goods[i]->setPrice(-1);
}
}
void Receipt::add(string name, int cost)
{
int place=0;
for (int i=0; i <500; i++)
{
if (goods[i]->getName()=="Empty" && goods[i]->getPrice()==-1)
{
place = i;
break;
}
}
goods[place]->setName(name);
goods[place]->setPrice(cost);
}
int Receipt::getPrice()
{
return price;
}
string Receipt::getName()
{
return good;
}
void Receipt::setName(string name_in)
{
good = name_in;
}
void Receipt::setPrice(int price_in)
{
price = price_in;
}
void Receipt::displayList()
{
//just displaying first item in list for debugging purposes
cout << goods[0]->getName() << endl << goods[0]->getPrice();
}
int main()
{
Receipt mine; //seg faults here
mine.add("banana", 50);
mine.displayList();
return 0;
}
your design is wrong, you have array of Receipt inside Receipt so when you initialize the object, it create 500 where each of them create another 500 endlessly. I think you want to create something like this instead
#include <iostream>
#include <string>
using namespace std;
class Receipt {
private:
int price;
string good;
public:
void setName(string name_in);
void setPrice(int price_in);
string getName();
int getPrice();
};
class Receipts {
private:
Receipt* goods[500]; //partially filled array
public:
Receipts();
void add(string name, int cost);
void displayList();
};
Receipts::Receipts()
{
for (int i = 0; i < 500; i++)
{
goods[i] = new Receipt();
goods[i]->setName("Empty");
goods[i]->setPrice(-1);
}
}
void Receipts::add(string name, int cost)
{
int place = 0;
for (int i = 0; i <500; i++)
{
if (goods[i]->getName() == "Empty" && goods[i]->getPrice() == -1)
{
place = i;
break;
}
}
goods[place]->setName(name);
goods[place]->setPrice(cost);
}
int Receipt::getPrice()
{
return price;
}
string Receipt::getName()
{
return good;
}
void Receipt::setName(string name_in)
{
good = name_in;
}
void Receipt::setPrice(int price_in)
{
price = price_in;
}
void Receipts::displayList()
{
//just displaying first item in list for debugging purposes
cout << goods[0]->getName() << endl << goods[0]->getPrice();
}
int main()
{
Receipts mine; //seg faults here
mine.add("banana", 50);
mine.displayList();
return 0;
}

Remove warning of deprecated conversion from string constant to 'char*

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;
}

How can I assign class object into a class

I need to creat an object Pokemon in the main()
that assign it into the class PokemonWorld, and let the PokemonWolrd to decide which PokemonStation is this Pokemon need to go
I tired get the data separatly (get name and hp) and get together(get a Pokemon class)
but both fail
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
class Pokemon {
public:
Pokemon() {};
Pokemon(char x[], int n) {
strncpy_s(name, x, 10);
hp = n;
};
private:
char name[10];
int hp;
};
class PokemonStation {
private:
Pokemon **list= new Pokemon*[1000];
public:
PokemonStation() {};
PokemonStation(int x) {
id = x;
};
int id;
void assigntoList(int i,Pokemon x)
{
if (i > 0)
i--;
list[i] = new Pokemon(x);
cout << "creat" << list[i];
};
};
class PokemonWorld {
private:
char name[10];
public:
PokemonStation s1;
PokemonStation s2;
PokemonWorld() {};
PokemonWorld(char x[], int y=1, int z=2) {
strncpy_s(name, x, 10);
PokemonStation s1(y);
PokemonStation s2(z);
};
const char* const getName() const{
return name;
};
void assigntoStation(int i,Pokemon x) {
if (i == 0 || i % 2 == 0)
s1.assigntoList(i, x);
else
s2.assigntoList(i, x);
};
};
void main() {
int number,hp,i;
char name[10];
cout << "What is the World Name ?" <<endl;
cin >> name;
PokemonWorld world(name);
cout << "Please input the number of Pokemon in the " << world.getName() <<" world:" << endl;
cin >> number;
Pokemon **mon = new Pokemon*[number];
cout << "Please input the characteristics of all Pokemon: Name HP" << endl;
for (i = 0;i < number;i++)
{
cin >> name >> hp;
mon[i] = new Pokemon(name, hp);
world.assigntoStation(i,*(mon[i]));
}
for (i = 0;i < number;i++)
cout << "world is " << world.getName() << endl;
system("pause");
};
In C++, you should use std::vectors for dynamic lists of things and std::strings for text. If you know Java, these are like ArrayList and String. (To use these, make sure you #include <vector> and <string>.)
For instance, your Pokemon class, rewritten with name as a string:
class Pokemon {
public:
Pokemon() {}
Pokemon(string name, int hp):name(name), hp(hp) { // construct the fields directly
}
private:
string name;
int hp;
};
And your PokemonStation class, rewritten using a vector for the list of Pokemon:
class PokemonStation {
private:
vector<Pokemon> list;
public:
PokemonStation() {}
PokemonStation(int x):id(x) {}
int id;
void assignToList(Pokemon x)
{
list.push_back(x); // add x to the list
}
};
If you want to print a Pokemon with cout <<, then you'll have to overload the << operator to define what gets printed. Add this into your class:
class Pokemon {
public:
...
friend ostream& operator<<(ostream& out, const Pokemon& p) {
out << p.name; // just print the name
return out;
}
...
};
Just make sure that you're couting a Pokemon, not a pointer-to-Pokemon (Pokemon*), and you won't get an address.

Errors in class relationships in c++

I am quite new to the concept of class relationships and wrote a program to test it out. However, it is giving me some errors.
There are 5 header files for classes University,Dept,Teacher,Student and Course and a .cpp file involved. I have implemented composition between University and Dept and bidirectional association between Dept and Student, Teacher, Course
uni.h
#pragma once
//#include"dept.h"
class University
{
public:
University(string,int);
void setDepartmentName(string,int);
Dept* getDeptAddress(int);
~University();
private:
Dept* departments;
int totalDepts;
string name;
};
University::University(string name1,int num) :name(name1)
{
departments = new Dept[num];
totalDepts=num;
}
void University::setDepartmentName(string name,int depNo)
{
departments[depNo].setName(name);
}
Dept* University::getDeptAddress(int i)
{
return &(departments[i]);
}
University::~University()
{
delete[] departments;
}
dept.h
#pragma once
//class Student;
//class Teacher;
//class Course;
#include"course.h"
#include"teacher.h"
#include"student.h"
class Dept
{
public:
Dept();
string getName();
void setName(string);
~Dept();
private:
Student** students;
Course** courses;
Teacher** teachers;
string name;
int noOfStudents, noOfTeachers, noOfCourses;
};
Dept::Dept()
{
}
string Dept::getName() {
return name;
}
void Dept::setName(string name1) {
name = name1;
}
Dept::~Dept()
{
}
course.h
#pragma once
class Dept;
//#include"dept.h"
class Course
{
public:
Course(string, string);
void assignDept(Dept*);
string getDeptName();
~Course();
private:
Dept* department;
string code;
string name;
};
Course::Course(string code1, string name1) :code(code1), name(name1)
{}
void Course::assignDept(Dept * dep)
{
department = dep;
}
string Course::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
return department->getName();
}
Course::~Course()
{}
student.h
#pragma once
#include"dept.h"
class Student
{
public:
Student(string,string);
void assignDept(Dept*);
string getDeptName();
~Student();
private:
Dept* department;
string rollNo, name;
};
Student::Student(string rNo,string name1):name(name1),rollNo(rNo)
{}
void Student::assignDept(Dept *dep)
{
department = dep;
}
string Student::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
return department->getName();
}
Student::~Student()
{
}
teacher.h
#pragma once
//#include"dept.h"
class Teacher
{
public:
Teacher(string);
void assignDept(Dept*);
string getDeptName();
~Teacher();
private:
Dept* department;
string name;
};
Teacher::Teacher(string name1) :name(name1)
{}
void Teacher::assignDept(Dept *dep)
{
department = dep;
}
string Teacher::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
return department->getName();
}
Teacher::~Teacher()
{
}
source.cpp
#include<iostream>
using namespace std;
#include<string>
#include"dept.h"
#include"course.h"
#include"teacher.h"
#include"student.h"
#include"uni.h"
int main()
{
University u1("FAST",3);
u1.setDepartmentName("CS", 0);
u1.setDepartmentName("EE", 1);
u1.setDepartmentName("CV", 2);
Student s1("l144049", "Syed Abdul Wahab");
Course c1("cs1", "ITC");
Teacher t1("abc");
c1.assignDept(u1.getDeptAddress(0));
t1.assignDept(u1.getDeptAddress(1));
s1.assignDept(u1.getDeptAddress(2));
cout << c1.getDeptName()<<endl;
cout << t1.getDeptName() << endl;
cout << s1.getDeptName() << endl;
cin.get();
return 0;
}
However, if i #include 'dept.h' in student.h, course.h and teacher.h, it gives me errors on 'Dept' namely 'identifier 'Dept' is undefined'.
Any help would me greatly appreciated!
The problem is you have a circular dependency: teacher.h includes dept.h which includes teacher.h. This can't work.
To fix it, use "forward declarations" in your header files, and move your implementations to .cpp files.

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;
}
}
}