i got a class for a date and a class for a point of time. Now i want to combine them. My problem is that i'm not able to get the output working, it always uses the initialized date.
Here is my code:
main.cpp
#include <iostream>
#include <iomanip>
#include "date.hpp"
#include "time.hpp"
using namespace std;
int main() {
Datum d1;
Datum d2(03, 12, 2015);
cout << "d1: " << d1 << endl;
cout << "d2: " << d2 << endl << endl;
zeit z1;
cout << "z1: " << z1 << endl;
zeit z2(d2, 23, 30);
cout << "z2: " << z2 << endl;
return 0;
}
date.cpp
#include "date.hpp"
#include <iostream>
Datum::Datum(unsigned int d, unsigned int m, unsigned int y)
{
day = d;
month = m;
year = y;
return;
}
Datum::Datum() : Datum(1, 1, 2000) { return; }
unsigned int Datum::getday() { return day; }
unsigned int Datum::getmonth() { return month; }
unsigned int Datum::getyear() { return year; }
std::ostream& operator<<(std::ostream& os, Datum& z)
{
os << z.getday() << ".";
os << z.getmonth() << ".";
os << z.getyear();
return os;
}
date.hpp
#ifndef DATUM_HPP_
#define DATUM_HPP_
#include <iostream>
class Datum {
private:
unsigned int day;
unsigned int month;
unsigned int year;
public:
Datum(unsigned int, unsigned int, unsigned int);
Datum();
unsigned int getday();
unsigned int getmonth();
unsigned int getyear();
friend std::ostream& operator<<(std::ostream&, Datum&);
};
#endif
time.cpp
#include "time.hpp"
#include <iostream>
zeit::zeit(Datum date, unsigned int h, unsigned int m)
{
std::cout << date.getday() << "." << date.getmonth() << "." << date.getyear() << std::endl;
min = m;
hour = h;
return;
}
zeit::zeit() : zeit(Datum(),0,0) { return; }
unsigned int zeit::getmin() { return min; }
unsigned int zeit::gethour() { return hour; }
std::ostream& operator<<(std::ostream& os, zeit& z)
{
os << z.date << ", ";
if (z.gethour() < 10)
os << "0" << z.gethour();
else
os << z.gethour();
os << ":";
if (z.getmin() < 10)
os << "0" << z.getmin();
else
os << z.getmin();
return os;
}
time.hpp
#ifndef ZEIT_HPP_
#define ZEIT_HPP_
#include "date.hpp"
class zeit {
private:
unsigned int min;
unsigned int hour;
Datum date;
public:
zeit(Datum, unsigned int, unsigned int);
zeit();
unsigned int getmin();
unsigned int gethour();
friend class Datum;
friend std::ostream& operator<<(std::ostream&, zeit&);
};
#endif
This is the output i get:
d1: 1.1.2000
d2: 3.12.2015
1.1.2000
z1: 1.1.2000, 00:00
3.12.2015
z2: 1.1.2000, 23:30
What am i doing wrong? Ty for any help!
You did not set the internal date of the zeit class, so it remains initialized with the default date when it gets called by the << operator. Add this line to the constructor of zeit in time.cpp:
this->date = date;
Related
I am trying to write a class with a map to keep a registry with unique ID for later accessing the objects. All compiled fine till i wrote the for loop in main trying to access objects of the class and their data. I am at a loss what is exactly wrong. I declared all static but doesn't work. I have been trying for several hours now but couldn't solve it. I know the problem is in the map as that is new for me, but i can't seem to find the issue. Hope someone sees what is wrong in my code.
#include <iostream>
#include <map>
#include <cassert>
#include <string>
#include <algorithm>
using namespace std;
class vertegenwoordiger{
public:
vertegenwoordiger(int id, string x, int y): ID(id), name(x),aantalpc(y) {
addtoregistry(this);
cout << "Vertegenwoordiger " << x << " is aangemaakt met " << y << " aantal verkochte pc's " << endl;
gemiddeldeverkoop = (gemiddeldeverkoop + y) / id;
}
static map<int, vertegenwoordiger*>registryMap; // PROBLEM HERE I GUESS
static void addtoregistry(vertegenwoordiger* object){
registryMap[object->ID] = object;
}
static void removefromregistry(vertegenwoordiger* object){
registryMap.erase(object->ID);
}
static vertegenwoordiger* findbymap(int id){
return registryMap[id];
} // MAYBE THIS FUNCTION IS NOT CORRECT ASWELL????
void commissionfixed (vertegenwoordiger* obj){
commissievast = obj->aantalpc*winstperpc;
}
void commissionextra (vertegenwoordiger*obj){
if (obj->aantalpc>gemiddeldeverkoop){
commissieplus = (obj->aantalpc - gemiddeldeverkoop) * 37;
}
}
static const int winstperpc;
static int gemiddeldeverkoop;
const int ID;
protected:
string name;
int aantalpc;
int commissievast;
int commissieplus;
};
const int vertegenwoordiger::winstperpc = 150;
int vertegenwoordiger::gemiddeldeverkoop = 0;
int main()
{
for (int i=0; i<4; i++){
string naam;
int pc;
cout << "geef naam in :";
cin >> naam;
cout << "geef aantal pc op :";
cin >> pc;
vertegenwoordiger* test = new vertegenwoordiger (i+1,naam,pc);
cout << "volgende aub : " << endl;
}
for (int i=1; i<4 ; i++){
vertegenwoordiger* val = vertegenwoordiger::findbymap(i); // I GUESS THE PROBLEM IS RELATED TO THIS LINE
vertegenwoordiger::commissionfixed (val);
vertegenwoordiger::commissionextra (val);
}
return 0;
}
#include <iostream>
#include <map>
#include <cassert>
#include <string>
#include <algorithm>
using namespace std;
class vertegenwoordiger{
public:
vertegenwoordiger(int id, string x, int y): ID(id), name(x),aantalpc(y){
addtoregistry(this);
cout << "Vertegenwoordiger " << x << " is aangemaakt met " << y << " aantal verkochte pc's " << endl;
gemiddeldeverkoop = (gemiddeldeverkoop + y) / id; }
static map<int, vertegenwoordiger*>registryMap;
static void addtoregistry(vertegenwoordiger* object){ registryMap[object->ID] = object; }
static void removefromregistry(vertegenwoordiger* object){ registryMap.erase(object->ID); }
static vertegenwoordiger* findbymap(int id){ return registryMap[id]; }
void commissionfixed (){
commissievast = this->aantalpc*winstperpc; }
void commissionextra (){
if (this->aantalpc>gemiddeldeverkoop){ commissieplus = (this->aantalpc - gemiddeldeverkoop) * 37; } }
void readCommission(){
cout << "comission payment is " << this->commissievast << endl;
}
static const int winstperpc;
static int gemiddeldeverkoop;
const int ID;
string name;
protected:
int aantalpc;
int commissievast;
int commissieplus;
};
map<int, vertegenwoordiger*>vertegenwoordiger::registryMap;
const int vertegenwoordiger::winstperpc = 150; int vertegenwoordiger::gemiddeldeverkoop = 0;
int main() {
for (int i=0; i<2; i++){ string naam; int pc; cout << "geef naam in :";
cin >> naam;
cout << "geef aantal pc op :";
cin >> pc;
vertegenwoordiger* test = new vertegenwoordiger (i+1,naam,pc);
cout << "next please : " << endl; }
for (int i=1; i<3 ; i++){
vertegenwoordiger* val = vertegenwoordiger::findbymap(i);
val->commissionfixed ();
val->commissionextra ();
val->readCommission ();
}
return 0; }
This project is not done but currently my group is stuck with the LNK1169 error.
We have a player.h and player.cpp as well as an enemy.h and enemy.cpp and obviously a source.cpp. Somehow the linking between the files got messed up when we combined work on the player and work on the enemy files.
Source.cpp
//#pragma once
#include "Player.h"
#include "Enemy.h"
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <random>
using namespace std;
int main()
{
cout << "Welcome to our game" << endl;
cout << endl << endl << endl;
int ans = 0;
do {
cout << " Main Menu" << endl;
cout << "-----------------------------" << endl;
cout << "1: Play Game" << endl;
cout << "-----------------------------" << endl;
cout << "2: Exit" << endl;
cout << "-----------------------------" << endl;
cin >> ans;
switch (ans)
{
case 1: //main body of game
case 2:
return 0;
default:
cout << "Please enter 1 to play the game or 2 to exit" << endl;
cin >> ans;
break;
}
} while (ans != 2);
return 0;
}
Enemy.h:
/* UML
Enemies
******************************************
Private
- Health: int
- Attack : int
- Defence : int
******************************************
Public
+ accessor and mutator functions
+ AttackCharacter()
+ DefendCharacter()
+ ChangePosition()
+ LoseHealth()
+ RandomSpawn()
*******************************************
*/
//#pragma once
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include <string>
using namespace std;
class Enemy
{
private:
int
health,
attack,
defence;
public:
Enemy(int Health, int Attack, int Defence)
{
health = Health; attack = Attack; defence = Defence;
}
int getHealth();
int getAttack();
int getDefence();
void setHealth(int h);
void setAttack(int a);
void setDefence(int d);
//void Attack(Player P1);
};
#endif
Enemy.cpp
#include "Enemy.h"
#include "Player.h"
/*#include <iostream>
#include <string>
using namespace std;
*/
int Enemy::getHealth() { return health; }
int Enemy::getAttack() { return attack; }
int Enemy::getDefence() { return defence; }
void Enemy::setHealth(int h)
{
health = h;
}
void Enemy::setAttack(int a)
{
attack = a;
}
void Enemy::setDefence(int d)
{
defence = d;
}
//void Enemy::Attack(Player P1)
//{
// int h = P1.getHealth();
// int d = P1.getDefence();
// int a = getAttack();
// if (d + h - a > h)
// {
// cout << "You lost 0 health" << endl;
// P1.setHealth(h);
// }
// else
// {
// int h1 = h + d - a;
// cout << "You lost " << h1 - h << " health" << endl;
// P1.setHealth(h1);
// }
//}
The Enemy::attack() function is a work in progress and not really the problem
Player.h:
//#pragma once
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Armor
{
string name;
char type;
int health;
int attack;
int defense;
Armor() { name = ""; type = ' '; health = 0; attack = 0; defense = 0; } // constructor
};
struct Weapon
{
string name;
char type;
int health;
int attack;
int defense;
Weapon() { name = ""; type = ' '; health = 0; attack = 0; defense = 0; } // constructor
};
struct Shield
{
string name;
char type;
int health;
int attack;
int defense;
Shield() { name = ""; type = ' '; health = 0; attack = 0; defense = 0; } // constructor
};
struct Potion
{
string name;
char type;
int health;
int attack;
int defense;
Potion() { name = ""; type = ' '; health = 0; attack = 0; defense = 0; } // constructor
};
vector<string> type = { "Bronze", "Iron", "Silver", "Steel", "Gold", "Diamond" };
class Player
{
private:
string name;
int initialhealth;
int initialattack;
int initialdefense;
int health;
int attack;
int defense;
public:
Player(string n = " ", int ih = 0, int ia = 0, int id = 0, int h = 0, int a = 0, int d = 0)
{
name = n; initialhealth = ih; initialattack = ia; initialdefense = id; health = h; attack = a; defense = d;
};
Armor armor;
Weapon weapon;
Shield shield;
Potion potion;
string getname();
int getinitialhealth();
int getinitialattack();
int getinitialdefense();
int getHealth();
int getAttack();
int getDefense();
void setname(string n);
void setinitialhealth(int ih);
void setinitialattack(int ia);
void setinitialdefense(int id);
void setHealth(int h);
void setAttack(int a);
void setDefense(int d);
void addITEMS();
void displayPlayer();
void checkARMOR();
};
#endif
Player.cpp:
//#include <iostream>
//#include <string>
#include "Player.h"
//using namespace std;
string Player::getname() { return name; }
int Player::getinitialhealth() { return initialhealth; }
int Player::getinitialattack() { return initialattack; }
int Player::getinitialdefense() { return initialdefense; }
int Player::getHealth() { return health; }
int Player::getAttack() { return attack; }
int Player::getDefense() { return defense; }
void Player::setname(string n) { name = n; }
void Player::setinitialhealth(int ih) { initialhealth = ih; }
void Player::setinitialattack(int ia) { initialattack = ia; }
void Player::setinitialdefense(int id) { initialdefense = id; }
void Player::setHealth(int ih) { health = ih; }
void Player::setAttack(int ia) { attack = ia; }
void Player::setDefense(int id) { defense = id; }
void Player::addITEMS()
{
health = initialhealth + armor.health + weapon.health + shield.health;
attack = initialattack + armor.attack + weapon.attack + shield.attack;
defense = initialdefense + armor.defense + weapon.defense + shield.defense;
}
void Player::displayPlayer()
{
cout << endl;
cout << "=========================" << endl;
cout << " Name : " << name << endl;
cout << " Health : " << health << endl;
cout << " Attack : " << attack << endl;
cout << " Defence : " << defense << endl;
cout << "=========================" << endl;
}
void Player::checkARMOR()
{
if (weapon.name == "Bronze")
{
armor.health = 10;
armor.attack = 5;
armor.defense = 15;
}
if (armor.name == "Iron")
{
armor.health = 100;
armor.attack = 15;
armor.defense = 150;
}
}
Any insight anyone could give into why the LNK1169 error may be popping up would be greatly appreciated. Thank you.
Its simple, you have used
#ifndef PLAYER_H
#define PLAYER_H
twice in Player.h and Enemy.h. Just simply replace:
#ifndef PLAYER_H
#define PLAYER_H
by
#ifndef ENEMY_H
#define ENEMY_H
in Enemy.h
Or use #pragma once preprocessor directive before your declarations in *.h
files
But the real problem is this line in Player.h
std::vector<std::string> type = { "Bronze", "Iron", "Silver", "Steel", "Gold", "Diamond" };
To declare an global variable in a header use the keyword extern.
// Player.h
extern std::vector<std::string> type;
// Player.cpp
std::vector<std::string> type = { "Bronze", "Iron", "Silver", "Steel", "Gold", "Diamond" };
Is it not an option to change this to an enum class?
enum class Types {
Bronze,
Iron,
Silver,
Steel,
Gold,
Diamond
};
And use namespace through out the application?
got a quick question. Why does the first programm work, but not the one when i split it into header/source file? How can i fix this? (error is that there is no default-constructor for "Stud s [line 6 main.cpp]")
Here is my code:
The working one:
#include <iostream>
#include <string>
using namespace std;
class Stud {
private:
string pren, surn;
unsigned int number;
public:
Stud(string p = "", string s = "", unsigned int n = 0) : pren(p), surn(s), number(n)
{
return;
}
friend ostream& operator<<(ostream& os, Stud& st) {
os << st.number << ", " << st.pren << " " << st.surn;
return os;
}
};
int main() {
Stud s;
Stud test("x", "y", 123);
cout << s << endl;
cout << test << endl;
}
Now the splitted one:
stud.hpp:
#ifndef STUD_HPP
#define STUD_HPP
#include <iostream>
#include <string>
class Stud {
private:
std::string pren, surn;
unsigned int number;
public:
Stud(std::string, std::string, unsigned int);
friend std::ostream& operator<<(std::ostream&, const Stud&);
};
#endif
stud.cpp:
#include "stud.hpp"
Stud::Stud(std::string p = "", std::string s = "", unsigned int n = 0) : pren(p), surn(s), number(n)
{
return;
}
std::ostream& operator<<(std::ostream& os, const Stud& st) {
os << st.number << ", " << st.pren << " " << st.surn;
return os;
}
main.cpp
#include <iostream>
#include "stud.hpp"
using namespace std;
int main() {
Stud s;
Stud test("x", "y", 123);
cout << s << endl;
cout << test << endl;
}
For whatever reason when I try to return a value from my overloaded function it is returning zero. I just started using overloading the other day so I am quite new to it, so if it is a simple mistake I am sorry about that.
It is quite strange because when I cout the values in the function, it shows exactly what I want to see, however when I return it, it still returns a empty value, which is zero.
The function is this:
double operator+=(double ls, Account& rs){
//cout << ls << endl;
//cout << rs._balance+ ls << endl;
return ls+=rs._balance;
//return rs._balance+=ls;
}
Here is the full .cpp file including the function:
#define _CRT_SECURE_NO_WARNINGS
#include <cstring>
#include <iomanip>
#include "Account.h"
using namespace std;
namespace sict{
Account::Account(){
_name[0] = 0;
_balance = 0;
}
Account::Account(double balance){
_name[0] = 0;
_balance = balance;
}
Account::Account(const char name[], double balance){
strncpy(_name, name, 40);
_name[40] = 0;
_balance = balance;
}
void Account::display()const{
for(int x = 0; x < 40; x++){
if(_name[x] == '\0')
x = 40;
else
cout << _name[x];
}
cout << ": $" << setprecision(2) << fixed << _balance;
}
Account Account::operator+(Account ls) {
return ls._balance + _balance;
}
double operator+=(double ls, Account& rs){
//cout << ls << endl;
//cout << rs._balance+ ls << endl;
return ls+=rs._balance;
//return rs._balance+=ls;
}
Account Account::operator+=(Account& ls){
return _balance+=ls._balance;
}
Account::Account(const char name[]){
strncpy(_name, name, 40);
}
char* Account::getName(){
return _name;
}
double Account::getBal(){
return _balance;
}
std::ostream& operator<<(std::ostream& ls, Account& rs){
rs.display();
return ls;
}
Account& Account::operator=(Account& ls){
if( !strcmp(ls._name,"") &&ls._balance > 0)
{
strcpy(_name, "Saving");
}
_balance = ls._balance;
//strcpy(_name, ls._name);
return *this;
}
char* Account::operator=(char* ls){
strcpy(_name, ls);
return _name;
}
}
Here is the header file:
#ifndef SICT_ACCOUNT_H__
#define SICT_ACCOUNT_H__
#include <iostream>
#include <cstring>
#include <iomanip>
namespace sict{
class Account{
char _name[41];
double _balance;
public:
Account();
Account::Account(double balance);
Account::Account(const char name[], double balance);
Account::Account(const char name[]);
Account& operator=(Account& ls);
Account operator+=(Account& ls);
char* operator=(char* ls);
void display()const;
double getBal();
char* getName();
friend double operator+=(double ls, Account& rs);
Account operator+(Account ls);
};
std::ostream& operator<<(std::ostream& ls, Account& rs);
};
#endif
And here is the main:
#include <iostream>
#include <string>
#include "Account.h"
using namespace sict;
using namespace std;
int main()
{
Account A;
Account B("Saving", 10000.99);
Account C("Checking", 100.99);
double value = 0;
cout << A << endl << B << endl << C << endl << "--------" << endl;
A = B + C;
A = "Joint";
cout << A << endl << B << endl << C << endl << "--------" << endl;
A = B += C;
cout << A << endl << B << endl << C << endl << "--------" << endl;
value += A;
value += B;
value += C;
cout << "Total balance: " << value << endl;
return 0;
}
For
value += A;
value += B;
value += C;
to work, you'll have to change the argument of the overloaded operator function to be a reference. Right now, it gets passed by value. The value gets modified in the function but that doesn't change the value in the calling function.
Instead of
double operator+=(double ls, Account& rs){
use
double operator+=(double& ls, Account& rs){
// ^^
In order for this to work you need to capture the left hand argument by reference.
double operator+=(double ls, Account& rs)
Needs to be
double operator+=(double& ls, Account& rs)
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)
{}