I have created a class called Book and it can have many Books. But I want to create a class shelf which can contain only 10 of Books if it is greater than than 10 ten it should print Error Message! But I cannot think of a way to make class shelf. So far I have done this :
#include <iostream>
#include <string>
using namespace std;
class Book{
private:
string bookName;
int pNum;
public:
Book();
Book(string tempName, int tNum){
setName(tempName);
setPageNum(tNum);
}
void setName(string bName){
bookName = bName;
}
void setPageNum(int tempNum){
pNum = tempNum;
}
string getName(){
return bookName;
}
int getPageNum(){
return pNum;
}
};
class Shelf{
public:
Book nBook[10];
void addbook();
void Book::addbook(Book nBook[10])
{
for(int i = 0; i<10; i++)
nBook[i] = nBook[i].setName(string bName)
}
};
int main(){
Book math = Book("math", 500);
Book abcd = Book("abcd", 501);
cout << English.getName() <<" "<<English.getPageNum()<<endl;
cout << German.getName() <<" "<<German.getPageNum()<<endl;
}
You can use the standard vector class to store the Book objects in the shelf.
In the example below, the book object will be copied when added to the Shelf
#include <vector>
using namespace std;
class Shelf{
public:
vector<Book> books;
bool addbook(Book book)
{
if(books.size() > 10)
{
return false;
}
else
{
books.push_back(book);
return true;
}
}
};
There are many ways to implement this. Note the default constructor of class Book. Let me know if you have any questions.
#include <iostream>
#include <string>
using namespace std;
class Book{
private:
string bookName;
int pNum;
public:
Book(){
}
Book(string tempName, int tNum){
setName(tempName);
setPageNum(tNum);
}
void setName(string bName){
bookName = bName;
}
void setPageNum(int tempNum){
pNum = tempNum;
}
string getName(){
return bookName;
}
int getPageNum(){
return pNum;
}
};
class Shelf{
public:
Book nBook[10];
int numberOfBooks;
void addbook(Book);
Shelf()
{
numberOfBooks = 0;
}
bool addBook(Book newBook)
{
if (numberOfBooks == 10)
{
return false;
}
else
{
nBook[numberOfBooks] = newBook;
numberOfBooks++;
return true;
}
}
};
int main(){
Book English = Book("math", 500);
Book German = Book("abcd", 501);
Shelf bookShelf = Shelf();
bookShelf.addBook(English);
}
Related
i know this is a simple problem solving but i'm still cannot figure it out. i'm trying to print out the input into an array by calling the function but its not working. Help me. below down here is the code. main.cpp and groovy.h. for your information, i am new to programming
Here is the output that i get
//main.cpp
#include <iostream>
#include "groovy.h"
#include <string>
#include <iomanip>
using namespace std;
void displayInventory(const groovy[], int);
int main() {
int s;
const int size = 20;
groovy car[size];
/*= {
groovy("M01", "Mazda CX5", 132403.00),
groovy("M02", "Mazda CX3", 126829.00),
groovy("M03", "Mazda 6 Grand Touring", 208408.00),
groovy("M04", "Mazda CX8", 173038.00),
};
cout<<"\nList of available car : \n"<<endl;
displayInventory(car, size);
*/
string code, model;
double price;
cout<<"\nPlease enter model details :- "<<endl;
cout<<"Model code : ";
cin>>code;
cout<<"Model name : ";
cin>>model;
cout<<"Model price : ";
cin>>price;
car[size].storeInfo(code, model, price);
displayInventory(car, size);
return 0;
}
void displayInventory(const groovy object[], int size){
for (int i = 0; i < size; i++){
cout<<setw(5)<<left<<object[i].getCode()
<<setw(28)<<left<<object[i].getModel()
<<"RM "<<right<<object[i].getPrice()<<endl;
}
}
//groovy.h
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class groovy{
private:
string modelCode;
string model;
double price;
public:
groovy(){
modelCode = "XXX";
model = " ";
price = 0.0;
}
groovy(string c,string m, double p){
modelCode = c;
model = m;
price = p;
}
void storeInfo(string c,string m, double p) {
modelCode = c;
model = m;
price = p;
}
string getCode() const {
string code = modelCode;
return code;
}
string getModel() const {
string m = model;
return m;
}
double getPrice() const {
double p = price;
return price;
}
};
car[size].storeInfo(code, model, price);
is bad because the array car has only size elements: car[0] to car[size-1].
The index must be not less than 0 and less than size.
Try
car[0].storeInfo(code, model, price);
instead.
I'm working on saving objects to a file and reading them back out. Right now I'm trying to do them one at a time to see if I can do and avoid duplicate objects. I'm getting this error: Exception thrown at 0x00915B18 in who.exe: 0xC0000005: Access violation writing location 0xDDDDDDDD
I know that this is because I'm not doing something with a pointer correctly but I'm lost and can't quite figure it out, current code is:
#include<iostream>
#include "Book.h"
#include "InventoryBook.h"
#include "SoldBook.h"
#include<string>
#include<fstream>
using namespace std;
void saveBook(Book);
Book returnBook();
void main() {
Book novel;
InventoryBook shelf;
SoldBook gone;
novel.setAuthor("Joe");
novel.setISBN("1234567788");
novel.setPublisher("Me");
novel.setTitle("Joe vs. the Volcano");
saveBook(novel);
Book rBook = returnBook();
cout << rBook.getAuthor();
system("Pause");
}
void saveBook(Book saved) {
ofstream myfile;
myfile.open("bookInventory.txt", ios::app);
myfile.write((char*)&saved, sizeof(saved));
}
Book returnBook() {
ifstream myfile;
myfile.open("bookInventory.txt", ios::in);
Book novel;
Book newBook;
myfile.read((char*)&novel, sizeof(novel));
newBook.setAuthor(novel.getAuthor());
return newBook;
}
I know that the save book function works and that I run into my issues with the returned book object. I'm just not able to see where this goes wrong. I'm a little fuzzy on pointers.
#pragma once
#ifndef BOOK_H
#define BOOK_H
#include<iostream>
using namespace std;
class Book{
private:
string ISBN;
string bookTitle;
string authorName;
string publisher;
public:
Book(string, string, string, string);
Book();
//destructor
~Book();
//Mutators
void setTitle(string);
void setISBN(string);
void setAuthor(string);
void setPublisher(string);
//accessors
string getTitle();
string getISBN();
string getAuthor();
string getPublisher();
};
#endif
#include "Book.h"
Book::Book() {
ISBN = "";
bookTitle = "";
authorName = "";
publisher = "";
}
Book::Book(string newISBN, string newTitle, string newAuthor, string newPub) {
ISBN = newISBN;
bookTitle = newTitle;
authorName = newAuthor;
publisher = newPub;
}
Book::~Book()
{
}
void Book::setISBN(string newISBN) {
ISBN = newISBN;
}
string Book::getISBN() {
return ISBN;
}
void Book::setTitle(string newTitle) {
bookTitle = newTitle;
}
string Book::getTitle() {
return bookTitle;
}
void Book::setAuthor(string newAuthor) {
authorName = newAuthor;
}
string Book::getAuthor() {
return authorName;
}
void Book::setPublisher(string newPub) {
publisher = newPub;
}
string Book::getPublisher() {
return publisher;
}
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;
}
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.
I have consulted many websites but have not successfully completed my code.
I am trying to write some code that spits out information about a car, once the user provides information. After compiling, the compiler says that "declaration does not declare anything." Here's the code:
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
Car::Car()
{
}
void Car::accel()
{
speed += 5;
}
void Car::brake()
{
speed -= 5;
}
void Car::setSpeed(int newSpeed)
{
speed = newSpeed;
}
int Car::getSpeed()
{
return speed;
}
void Car::setMake(string newMake)
{
make = newMake;
}
string Car::getMake()
{
return make;
}
void Car::setYearModel(int newYearModel)
{
yearModel = newYearModel;
}
int Car::getYearModel()
{
return yearModel;
}
int main()
{
Car auto; //instance of class Car
int autoYear; //year of auto
string autoMake; //make of auto
int autoSpeed; //speed of auto
cout << "Enter the year model of your car. ";
cin >> autoYear;
cout << "Enter the make of your car. ";
cin >> autoMake;
auto.setYear(autoYear); //stores input year
auto.setMake(autoMake); //stores input make
}
And the header, Car.h:
#include <iostream>
#include <string>
using namespace std;
#ifndef CAR_H
#define CAR_H
class Car
{
private:
int yearModel;
string make;
int speed;
public:
Car();
void accel();
void brake();
void setSpeed(int newSpeed);
int getSpeed();
void setMake(string newMake);
string getMake();
void setYearModel(int newYearModel);
int getYearModel();
};
#endif
I also have errors for missing semicolons every time my object auto appears ("expected ; before auto" and "expected primary-expression before auto"). What could be the problem?
auto is a keyword. You'll need to pick a different name.