I got a trouble while compiling the following source code
[linker error] undefined reference to 'dish::dish()'
[linker error] undefined reference to 'dish::~dish()'
[linker error] undefined reference to 'dish::ShowResult()' Can anybody help me?
THE HEADER FILE(dish.h):
#ifndef DISH_H
#define DISH_H
class dish {
public:
dish();
dish(std::string name, std::string variety, float caloric, float price);
~dish();
static int GetN();
void SetN(int N);
static int IncrementN();
std::string GetName() const;
void SetName(std::string name);
std::string GetVariety() const;
void SetVariety(std::string variety);
float GetCaloric() const;
void SetCaloric(float caloric);
float GetPrice() const;
void SetPrice(float price);
void Enter();
void ShowResult();
private:
std::string name;
std::string variety;
float caloric;
float price;
static int N;
};
int dish::N;
#endif
and the dish.cpp:
#include <iostream>
#include <cstring>
#include "dish.h"
dish::dish()
{
dish::Enter();
}
dish::dish(std::string name, std::string variety, float caloric, float price)
{
this->name = name;
this->variety = variety;
this->caloric = caloric;
this->price = price;
}
dish::~dish()
{
}
static int dish::GetN()
{
return N;
}
void dish::SetN(int N)
{
this->N = N;
}
static int dish::IncrementN()
{
N++;
}
std::string dish::GetName() const
{
return name;
}
void dish::SetName(std::string name)
{
dish::name = name;
}
std::string dish::GetVariety() const
{
return variety;
}
void dish::SetVariety(std::string variety)
{
dish::variety = variety;
}
float dish::GetCaloric() const
{
return caloric;
}
void dish::SetCaloric(float caloric)
{
this->caloric = caloric;
}
float dish::GetPrice() const
{
return price;
}
void dish::SetPrice(float price)
{
this->price = price;
}
void dish::Enter()
{
std::cout << "\n \\*_________________________________*\\\n";
std::cout << "\n ENTER THE NAME OF DISH: ";
getline(std::cin, name);
std::cout << " ENTER THE VARIETY: ";
getline(std::cin, variety);
std::cout << " ENTER THE CALORIC CONTENT: ";
(std::cin >> caloric).get();
std::cout << " ENTER THE PRICE: ";
(std::cin >> price).get();
std::cout << "\n \\*_________________________________*\\\n";
dish::IncrementN();
}
void dish::ShowResult()
{
std::cout << "\n \\*________________________*\\\n";
std::cout << "\n THE NAME OF DISH: " << dish::GetName() << std::endl;
std::cout << " THE VARIETY: " << dish::GetVariety() << std::endl;
std::cout << " THE CALORIC CONTENT: " << dish::GetCaloric() << std::endl;
std::cout << " THE PRICE: " << dish::GetPrice() << std::endl;
std::cout << "\n \\*________________________*\\\n";
}
Implementation in the main...
#include <cstring>
using namespace std;
#include "dish.h"
int main() {
dish a;
a.ShowResult();
return 0;
}
You should link dish.cpp with your main executable i.e. add dish.cpp in your project.
This is a syntax error which produces compile errors on dish.cpp . The linkage error is just a consequence of that.
Remove the static keywords from dish.cpp .
Keep in mind that the only interesting error is almost always the very first one.
Explanation: in order to do class methods, write static only in the header file, not in the .cpp .
For information but this is obviously not what you were trying to do: static in a .cpp can be used to make a function invisible outside a compile unit, but is actually a deprecated construct inherited from C. You should prefer anonymous namespaces.
Related
I'm trying to learn how to use class in cpp, and one of the activities had me make a class header and a separate cpp file for implementation.
here are my codes:
main.cpp
#include <iostream>
#include <string>
#include "studentType.h"
using namespace std;
int main()
{
studentType student;
studentType newStudent("Brain", "Johnson", '*', 85, 95, 3.89);
student.print();
cout << "***************" << endl << endl;
newStudent.print();
cout << "***************" << endl << endl;
return 0;
}
studentType.h
#ifndef CODINGPROJECTS_STUDENTTYPE_H
#define CODINGPROJECTS_STUDENTTYPE_H
#include <string>
using namespace std;
class studentType {
private:
string firstName;
string lastName;
char courseGrade;
int testScore;
int programmingScore;
double GPA;
public:
//GETTERS
string getfirstName() const;
string getlastName() const;
char getcourseGrade() const;
int gettestScore() const;
int getprogrammingScore() const;
double getGPA() const;
//SETTERS
void setfirstName(string name);
void setlastName(string name);
void setcourseGrade(char course);
void settestScore(int test);
void setprogrammingScore(int programming);
void setGPA(double gpa);
studentType(string first, string last, char course, int test, int programming, double gpa);
studentType();
void print();
};
#endif
studentTypeImp.cpp
#include <iostream>
#include "studentType.h"
using namespace std;
studentType::studentType(string first, string last, char course, int test, int programming, double gpa){
}
void studentType::setfirstName(string first){
firstName = first;
}
void studentType::setlastName(string last){
lastName = last;
}void studentType::setcourseGrade(char course) {
courseGrade = course;
}
void studentType::settestScore(int test) {
testScore = test;
}
void studentType::setprogrammingScore(int programming){
programmingScore = programming;
}
void studentType::setGPA(double gpa){
GPA = gpa;
}
string studentType::getfirstName() const{ return firstName;}
string studentType::getlastName() const{ return lastName;}
char studentType::getcourseGrade() const{return courseGrade;}
int studentType::gettestScore() const{ return testScore;}
int studentType::getprogrammingScore() const{ return programmingScore;}
double studentType::getGPA() const{return GPA; }
void studentType::print(){
cout << "Name: " << getfirstName() << " " << getlastName() << endl;
cout << "Grade: " << getcourseGrade() << endl;
cout << "Test Score: " << gettestScore() << endl;
cout << "Programming Score: " << getprogrammingScore() << endl;
cout << "GPA: " << getGPA() << endl;
}
I have an inkling that it has to do with my header file but I don't know where to start.
Add a definition for that default constructor which you declared in the header file. One way to do this is to add this code to studentTypeImp.cpp:
studentType::studentType() {
}
Add a defalut constructor and use the command g++ main.cpp studentTypeImp.cpp to have a try.
When I tried to convert it the class result can't get the data from class marks even if I made the data variables of marks public I don't know why. I also declared a object of class marks in result and then tried to access it but failed again I am new to coding so don't know all the syntax correctly your help will be of great use
#include <string.h>
#include <iostream>
using namespace std;
class student {
private:
int rl;
char nm[20];
public:
void read();
void display();
};
class marks : public student {
protected:
int s1;
int s2;
int s3;
public:
void getmarks();
void putmarks();
};
class result : public marks {
private:
int t;
float p;
char div[10];
public:
void process();
void printresult();
};
void student::read() {
cout << "enter Roll no and Name " << endl;
cin >> rl >> nm;
}
void student::display() {
cout << "Roll NO:" << rl << endl;
cout << "name : " << nm << endl;
}
void marks ::getmarks() {
cout << "enter three subject marks " << endl;
cin >> s1 >> s2 >> s3;
}
void marks ::putmarks() {
cout << "subject 1:" << s1 << endl;
cout << "subject 2 :" << s2 << endl;
cout << "subject 3:" << s3 << endl;
}
void result::process() {
t = s1 + s2 + s3;
p = t / 3.0;
p >= 60 ? strcpy(div, "first")
: p >= 50 ? strcpy(div, "second")
: strcpy(div, "third");
}
void result::printresult() {
cout << "total = " << t << endl;
cout << "per = " << p << "%" << endl;
cout << "div = " << div << endl;
}
int main(){
result x;
x.read();
x.getmarks();
x.process();
x.display();
x.putmarks();
x.printresult();
}
#include<iostream>
#include<string.h>
using namespace std;
class marks // parent class
{
protected:
int s1;
int s2;
int s3;
public:
void getmarks();
void putmarks();
};
class student : public marks // child class
{
private:
int rl;
char nm[20];
public:
void read();
void display();
};
class result : public marks// child class
{
private:
int t;
float p;
char div[10];
public:
void process();
void printresult();
};
void student::read()
{
cout<<"enter Roll no and Name "<<endl;
cin>>rl>>nm;
}
void student:: display()
{
cout <<"Roll NO:"<<rl<<endl;
cout<<"name : "<<nm<<endl;
}
void marks ::getmarks()
{
cout<<"enter three subject marks "<<endl;
cin>>s1>>s2>>s3;
}
void marks ::putmarks()
{
cout <<"subject 1:"<<s1<<endl;
cout<<"subject 2 :"<<s2<<endl;
cout <<"subject 3:"<<s3<<endl;
}
void result::process()
{
t= s1+s2+s3;
p = t/3.0;
p>=60?strcpy(div,"first"):p>=50?strcpy(div, "second"): strcpy(div,"third");
}
void result::printresult()
{
cout<<"total = "<<t<<endl;
cout<<"per = "<<p<<"%"<<endl;
cout<<"div = "<<div<<endl;
}
int main()
{
result x;
student y;
y.read();
x.getmarks();
x.process();
y.display();
x.putmarks();
x.printresult();
}
Compiles and everything except adding in a 'ship'
Constructor for ship
Ship::Ship(std::string name, int length, std::string show) {
std::string _name = name;
int _length = length;
std::string _show = show;
}
void Ships::buildShip() {
std::string name, show;
int length = 0;
std::cout << "What is the name of the ship? ";
std::cin >> name;
std::cout << "How long is the ship in feet? ";
std::cin >> length;
std::cout << "What show/movie is the ship from? ";
std::cin >> show;
std::cout << std::endl;
Ship ship(name, length, show);
addShip(ship);
}
void Ships::addShip(Ship &ship) {
ships.push_back(ship);
}
I'm sure it's something very obvious, I've searched the web and found nothing helpful. I only took snippets from my code if anything else is needed let me know. Thanks in advance!
/Ship.h
#pragma once
#include <string>
class Ship {
std::string _name;
int _length;
std::string _show;
public:
Ship(){
std::string name = _name;
int length = _length;
std::string show = _show;
};
Ship(std::string _name, int _length, std::string _show);
std::string getName();
int getLength();
std::string getShow();
};
/Ship.cpp
#include <string>
#include "Ship.h"
Ship::Ship(std::string name, int length, std::string show) {
std::string _name = name;
int _length = length;
std::string _show = show;
}
std::string Ship::getName() {
return _name;
}
int Ship::getLength() {
return _length;
}
std::string Ship::getShow() {
return _show;
}
/Ships.h
#pragma once
#include <vector>
#include "Ship.h"
class Ships {
std::vector<Ship> ships;
public:
void addShip(Ship &ship);
int getCount();
Ship getLongestShip();
void buildShip();
int getNumberOfShipsLongerThan(int input);
void displayShips();
};
/Ships.cpp
#include "Ships.h"
#include <iostream>
void Ships::addShip(Ship &ship) {
ships.push_back(ship);
}
int Ships::getCount() {
return ships.size();
}
Ship Ships::getLongestShip() {
Ship longestShip = ships[0];
for (Ship aShip : ships) {
if (longestShip.getLength() < aShip.getLength())
longestShip = aShip;
}
std::cout << "The longest ship is the " << longestShip.getName() << std::endl;
std::cout << "From end to end the length is " << longestShip.getLength() << std::endl;
std::cout << "The show/movie it is from is " << longestShip.getShow() << std::endl;
return longestShip;
}
int Ships::getNumberOfShipsLongerThan(int input) {
int longerThan = 0;
int size = ships.size();
for (int i = 0; i < size; i++) {
if (input < ships[i].getLength())
longerThan++;
}
return longerThan;
}
void Ships::displayShips() {
std::cout << " Complete Bay Manifest " << std::endl;
std::cout << "***********************" << std::endl;
for (int i = 0; i < ships.size(); i++) {
int a = i + 1;
std::cout << "Ship (" << a << ")" << std::endl;
std::cout << "Name: " << ships[i].getName() << std::endl;
std::cout << "Length: " << ships[i].getLength() << std::endl;
std::cout << "Show: " << ships[i].getShow() << std::endl<<std::endl;
}
}
void Ships::buildShip() {
std::string name, show;
int length = 0;
std::cout << "What is the name of the ship? ";
std::cin >> name;
std::cout << "How long is the ship in feet? ";
std::cin >> length;
std::cout << "What show/movie is the ship from? ";
std::cin >> show;
std::cout << std::endl;
Ship ship(name, length, show);
addShip(ship);
}
/driver.cpp
#include <iostream>
#include "Ship.h"
#include "Ships.h"
void menu();
Ship buildShip();
void shipsInBay(Ships &ships);
void processDirective(int choice, Ships &ships);
void longerThan(Ships &ships);
int main() {
std::cout << "Welcome to Daniel Mikos' Ship Bay!" << std::endl << std::endl;
menu();
system("PAUSE");
return 0;
}
void menu() {
Ships ships;
int choice = 0;
std::cout << "Please make a selection" << std::endl;
std::cout << " (1) Add a ship to the bay" << std::endl;
std::cout << " (2) How many ships are already in the bay?" << std::endl;
std::cout << " (3) Which ship is the longest? " << std::endl;
std::cout << " (4) Ships longer than ___? " << std::endl;
std::cout << " (5) Manifest of all ships currently logged" << std::endl;
std::cout << " (6) Exit" << std::endl;
std::cout << " Choice: ";
std::cin >> choice;
processDirective(choice, ships);
}
Ship buildShip() {
std::string name, show;
int length = 0;
std::cout << "What is the name of the ship? ";
std::cin >> name;
std::cout << "How long is the ship in feet? ";
std::cin >> length;
std::cout << "What show/movie is the ship from? ";
std::cin >> show;
std::cout << std::endl;
Ship ship(name, length, show);
return ship;
}
void shipsInBay(Ships &ships) {
int count = ships.getCount();
std::cout << std::endl;
std::cout << "There is currently " << count;
std::cout << " ship(s) in bay" << std::endl << std::endl;
}
void longerThan(Ships &ships) {
int input = 0;
std::cout << "Find ships longer than? ";
std::cin >> input;
std::cout << "There are " << ships.getNumberOfShipsLongerThan(input) << "longer than " << input << "feet";
}
void processDirective(int choice, Ships &ships) {
if (choice == 1)
buildShip();
if (choice == 2)
shipsInBay(ships);
if (choice == 3)
ships.getLongestShip();
if (choice == 4)
longerThan(ships);
if (choice == 5)
ships.displayShips();
menu();
}
There is all of my code
To add an object to a vector of objects use emplace_back() and pass the constructor arguments as arguments to emplace back. It will then build the object in place so call:
ships.emplace_back(name, length, show);
to construct your ship object in place. Your constructor also is incorrect as pointed out by #Kevin. You need to change it to:
this->_name = name;
this->_length = length;
this->_show = show;
assuming I've guessed your class design correctly.
EDIT
Tried to build a minimum example from this and this works fine for me:
Header.h
class Ship {
std::string _name;
int _length;
std::string _show;
public:
Ship();
Ship(std::string _name, int _length, std::string _show);
std::string getName();
int getLength();
std::string getShow();
};
class Ships {
public:
void addShip(Ship &ship);
void buildShip();
std::vector<Ship> ships;
};
and Source.cpp
#include "Header.h"
Ship::Ship(std::string name, int length, std::string show) {
this->_name = name;
this->_length = length;
this->_show = show;
}
void Ships::buildShip() {
std::string name = "Name";
std::string show = "Show";
int length = 0;
ships.emplace_back(name, length, show);
}
int main(int argc, char argv[])
{
Ships ships;
ships.buildShip();
std::cout << "Number of ships = " << ships.ships.size() << std::endl;
return 0;
}
Prints Number of ships = 1. Can you slim it down to something like this?
i am trying to input a string in my code in c++ and when i run i always get the following error: Exception thrown at 0x0F5023F5 (msvcp140d.dll) in assignment-1.exe: 0xC0000005: Access violation writing location 0x00229C20. i will post my code below if anyone could help me that would be great.Please note that i already know that its a problem with me trying to access the memory location on which you dont have access to, i just dont know how to fix it.
HEADER FILE:
#ifndef item_H
#define item_h
class item
{
private:
//attributes
int itemID;
char itemName[20];
float itemcost;
float itemprice;
//utility function
float calcPrice();
public:
//constructor
item(int = 000, char[] = "itemUnknown", float = 0,float = 0);
//destructor
~item();
//set functions
void setAll(int, char[], float, float);
void setID(int);
void setName(char[]);
void setCost(float);
//get function
int getID();
float getcost();
float getprice();
void getname();
//print function
void print();
};
#endif
CPP:
#include "Dariush.h"
#include <iostream>
#include <iomanip>
#include<string>
using namespace std;
//constructor will set attributes
item::item(int ID, char n[] , float c,float p)
{
setID(ID);
setName(n);
setCost(c);
setAll(ID, n, c, p);
}
//destructor will print destroing two objects
item::~item()
{
cout << "destroing two objects : " << " " << itemName << " "
<< " & " << itemName << endl;
}
//set functions :
void item::setID(int ID)
{
cout << "please enter the item's ID : " << endl;
cin >> ID;
}
void item::setName(char n[])
{
cout << "please enter the item's name" << endl;
cin.ignore();
cin.getline(n, 20);
}
void item::setCost(float c)
{
cout << "please enter the item's cost : " << endl;
cin >> c;
}
void item::setAll(int ID, char n[], float c, float p)
{
itemID = (ID > 0 && ID < 999) ? ID : 0;
strcpy_s(itemName, n);
itemcost = (c > 0) ? c : 0;
calcPrice();
}
//get functions :
int item::getID()
{
return itemID;
}
float item::getcost()
{
return itemcost;
}
float item::getprice()
{
return itemprice;
}
void item::getname()
{
cout << itemName << endl;
}
//print function :
void item::print()
{
cout << "ID : " << itemID << endl
<< "Name : " << itemName << endl
<< "cost : " << itemcost << endl
<< "price : " << itemprice << endl;
}
// utility function for price callculation :
float item::calcPrice()
{
if (itemcost < 1000)
{
itemprice = itemcost + (itemcost*0.1);
}
else
itemprice = itemcost + (itemcost*0.2);
return itemprice;
}
MAIN.CPP:
#include "Dariush.h"
#include <iostream>
#include<string>
using namespace std ;
void main()
{
item i1;
item i2;
i1.print();
i2.print();
}
thanks for the assistance.
Lets take a closer look at these three function declarations:
item(int = 000, char[] = "itemUnknown", float = 0,float = 0);
void setAll(int, char[], float, float);
void setName(char[]);
The thing here is that the character "array" arguments you declare are not really arrays at all. Instead they are pointers. When declaring arguments, e.g. char n[] is actually translated by the compiler as char *n.
The constructor declaration makes the pointer point to the constant string literal "". And the important thing about constant string literals is that they are indeed constant. Trying to modify a string literal leads to undefined behavior. And change this literal is what you are trying to do with the cin.getline(n, 20) call in the setName function. Not only that, but you are also telling the cin.getline function to read more than fits in the string literal.
The simple solution is to have setName read into the member variable itemName instead.
There are many problems with this code, but the one that is causing the access violation is:
void item::setName(char n[])
{
cout << "please enter the item's name" << endl;
cin.ignore();
cin.getline(n, 20); //here
}
You should use cin.getline(itemName, 20); instead.
Also, to prevent such error in the future, declare arguments as char const n[] instead of char n[] - good compiler should display a warning when you use string literals with non-const pointer as argument.
I'm getting a LNK 2019 and 2001 error every time I compile. LNK2019 states:
public: __thiscall ColMbr::ColMbr(unsigned int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0ColMbr##QAE#IV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) referenced in function "public: __thiscall Alumni::Alumni(unsigned int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,int,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Alumni##QAE#IV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##HHH0#Z
So there is some kind of error linking ColMbr class with Alumni. LNK2011 says:
"public: virtual void __thiscall Alumni::addClass(unsigned int,unsigned int)" (?addClass#Alumni##UAEXII#Z)
So there's an issue with my virtual function call. I understand that the LNK errors means there was a variable that needed to be declared that I missed but I don't see it. Firstly, the Alumni::addClass function is just there to make sure Alumni does not become an abstract class like ColMbr, which it's derived from. Secondly, all of the arguments in Alumni are defined and declared either in Alumni or in ColMbr.
If it was anything I'd say LNK2019 is probably a problem with my const unsigned int idNbr. I don't know what's wrong with LNK2001. Maybe I need to give the function a garbage purpose or something.
This is my header file followed by the cpp.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#ifndef _ColMbr
#define _ColMbr
class ColMbr
{
protected:
const unsigned int idNbr;
std::string name;
public:
ColMbr();
ColMbr(const unsigned int idNbr, std::string stdnt_name);
std::string setName(std::string stdnt_name);
virtual void display(void);
virtual void addClass(unsigned int credits, unsigned int gradePoint) = 0;
};
#endif // !1
std::string ColMbr::setName(std::string stdnt_name)
{
name = stdnt_name;
return name;
}
class Student : public ColMbr
{
private:
unsigned int credHrs, qualPts;
std::string degSought;
double GPA;
public:
Student(unsigned int idNbr, std::string name) : ColMbr (idNbr, name)
{
credHrs = 0;
qualPts = 0;
degSought = "Unspecified";
}
Student(const unsigned int idNbr, std::string stdnt_name, unsigned int credHrs1, unsigned int qualPts1, std::string newDegree) : ColMbr(idNbr,stdnt_name)
{
credHrs = credHrs1;
qualPts = qualPts1;
degSought = newDegree;
}
void setDegree(std:: string newDegree);
double getGPA(unsigned int credHrs, unsigned int qualPts);
void addClass(unsigned int newClass, unsigned int newQualPts)
{
credHrs = credHrs + newClass;
qualPts = qualPts + newQualPts;
}
void display(void)
{
std::cout << "This student is active." << std::endl <<"ID #: "
<< idNbr << std::endl << "Name: " << name << std::endl
<< "GPA: " << GPA << std::endl << "Major: " << degSought
<< std::endl;
}
};
void Student::setDegree(std:: string newDegree)
{
degSought = newDegree;
}
double Student::getGPA(unsigned int credHrs, unsigned int qualPts)
{
double credHrs1, qualPts1;
std::istringstream input(credHrs);
input >> credHrs1;
std::istringstream input1(qualPts);
input >> qualPts1;
GPA = qualPts1 / credHrs1;
return GPA;
}
#ifndef _Alumni
#define _Alumni
class Alumni : public ColMbr
{
private:
std::string degree;
int month, day, year;
public:
Alumni(const unsigned int idNbr, std::string stdnt_name, int gradMonth, int gradDay, int gradYear, std::string newDegree) : ColMbr(idNbr, stdnt_name)
{
degree = newDegree;
month = gradMonth;
day = gradDay;
year = gradYear;
}
void addClass(unsigned int credits, unsigned int gradePoint);
void display (void)
{
std::cout << "This student is an Alumni." << std::endl <<"ID #: "
<< idNbr << std::endl << "Name: " << name << std::endl
<< "Graduation Date: " << month << "/" << day << "/" << year
<< std::endl << "Major: " << degree << std::endl;
}
};
#endif
/**********************************************
#include <iostream>
#include <cstdlib>
#include "ColMbrs.h"
int main()
{
ColMbr *cMbr[4]; // array of base-class pointers
int i; // index to array
// Create some college members
cMbr[0] = new Student( 12345, "Steven DiFranco", 15, 33, "AA" );
cMbr[1] = new Alumni( 98765, "Don Green", 12, 15, 1978, "AAS" );
cMbr[2] = new Alumni( 24680, "Henry Thoreau", 5, 22, 1846, "AA" );
cMbr[3] = new Student( 13579, "Millenia Best" );
// display the array
cout << "All college members:\n";
for( i = 0; i < 4; ++i )
{
cMbr[i]->display(); // no need to check type field or cast
cout << endl;
}
// test addClass for student
cMbr[3]->addClass( 3, 12 );
cMbr[3]->display();
cout << endl;
system("PAUSE");
return 0;
}
So, this is how I got my code to compile and run correctly.
I declared both the constructor and the function that were giving me problems outside the class. After that I had to put an assignment for GPA from the GPA function into the Student::display(void) function. Finally, I couldn't figure out how to initialize the const unsigned in idNbr in the ColMbr class from the ColMbr(const unsigned int idNbr, std::string stdnt_name) constructor so I took the const label off the variable.
Here's the code for the header file. The .cpp remains the same.
Edited to reflect how I solved const unsigned int idNbr problem.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#ifndef _ColMbr
#define _ColMbr
class ColMbr
{
protected:
unsigned const int idNbr;
std::string name;
public:
//A default and initial constructor are called.
ColMbr();
ColMbr(const unsigned int id, std::string stdnt_name) : idNbr(id)
{
name = setName(stdnt_name);
}
virtual ~ColMbr();
std::string setName(std::string stdnt_name);
//ColMbr is made into an abstract class. It can not be called except with
//a pointer.
virtual void display(void);
virtual void addClass(unsigned int credits, unsigned int gradePoint) = 0;
};
#endif // !1
ColMbr::~ColMbr(){}
void ColMbr::display(void)
{
}
std::string ColMbr::setName(std::string stdnt_name)
{
name = stdnt_name;
return name;
}
class Student : public ColMbr
{
private:
unsigned int credHrs, qualPts;
std::string degSought;
double GPA;
public:
//A constructor with no additional Student attributes is created.
Student(unsigned int idNbr, std::string name) : ColMbr (idNbr, name)
{
credHrs = 0;
qualPts = 0;
degSought = "Unspecified";
}
//A constructor that adds Student attributes is created.
Student(const unsigned int idNbr, std::string stdnt_name, unsigned int credHrs1, unsigned int qualPts1, std::string newDegree) : ColMbr(idNbr,stdnt_name)
{
credHrs = credHrs1;
qualPts = qualPts1;
degSought = newDegree;
}
~Student();
//Functions for initializing the variables that will be output.
void setDegree(std:: string newDegree);
static double getGPA(unsigned int credHrs, unsigned int qualPts);
//The virtual functions are overwritten. Addclass allows for a change in GPA.
void addClass(unsigned int newClass, unsigned int newQualPts)
{
credHrs = credHrs + newClass;
qualPts = qualPts + newQualPts;
}
void display(void)
{
GPA = getGPA(credHrs, qualPts);
std::cout << "This student is active." << std::endl <<"ID #: "
<< idNbr << std::endl << "Name: " << name << std::endl
<< "GPA: " << GPA << std::endl << "Major: " << degSought
<< std::endl;
}
};
Student::~Student(){}
void Student::setDegree(std:: string newDegree)
{
degSought = newDegree;
}
//getGPA is declared and new students receive a 0
double Student::getGPA(unsigned int credHrs, unsigned int qualPts)
{
double GPA;
double credHrs1, qualPts1;
if (credHrs == 0)
{
GPA = 0;
}
else
{
credHrs1 = static_cast<double>(credHrs);
qualPts1 = static_cast<double>(qualPts);
GPA = qualPts1 / credHrs1;
}
return GPA;
}
#ifndef _Alumni
#define _Alumni
class Alumni : public ColMbr
{
private:
std::string degree;
int month, day, year;
public:
//An alumni constructor is defined.
Alumni(const unsigned int idNbr, std::string stdnt_name, int gradMonth, int gradDay, int gradYear, std::string newDegree) : ColMbr(idNbr, stdnt_name)
{
degree = newDegree;
month = gradMonth;
day = gradDay;
year = gradYear;
}
~Alumni();
//The virtual classes are overwritted.
//Even though addClass has no function here it is overwritten to ensure Alumni is not an abstract class.
void addClass(unsigned int credits, unsigned int gradePoint);
void display (void)
{
std::cout << "This student is an Alumni." << std::endl <<"ID #: "
<< idNbr << std::endl << "Name: " << name << std::endl
<< "Graduation Date: " << month << "/" << day << "/" << year
<< std::endl << "Major: " << degree << std::endl;
}
};
#endif
Alumni::~Alumni(){}
void Alumni::addClass(unsigned int credits, unsigned int gradePoint)
{}