Defining an Array of type Class - c++

I'm trying to define an array of type Class, for my homework. The classB and classC is defined inside another classA, and I have to define the an Array which
is defined inside classC of type classB. Below is the code I'm writing.
//main.cpp
...
//cop.h
class cop
{
public:
....
class Person
{
private:
static char name;
static char age;
static char gender;
};
class Station
{
public:
Station();
~Station();
private:
Person personArray[20];
protected:
void visit();
};
//cop.cpp
char cop::Person::name;
char cop::Person::age;
char cop::Person::gender;
cop::Station::Station(){}
cop::Station::~Station(){}
Person cop::Station::personArray[20];
I get following ERROR;
'Person' does not name a type

First of all (as I'm pointing out later) the fields of Person should not be static. After that, remove the following lines:
char cop::Person::name;
char cop::Person::age;
char cop::Person::gender;
Person cop::Station::personArray[20];
Properly designed your code should read like the following:
// Person.h
class Person
{
public:
char name;
char age;
char gender;
};
// Station.h
class Station
{
public:
Station();
~Station();
private:
Person personArray[20];
protected:
void visit();
};
// Station.cpp
Station::Station()
{
for (int i = 0; i < 20; i++)
{
personArray[i].age = ...;
}
}
By the way: declaring all fields of the Person class static will definitely make sure that all persons in your array have the same name, age and gender...

Related

How do I create a sub class in C++

I am new to C++. I am trying to create a subclass of a base class, but it does not seem to work.
I have the base class Person and a sub class Student, and the Person class is complete. Now I want to create a sub class Student. I have the following code:
namespace Uni
{
class Student : public Person
{
private:
int enrollNumber_;
string majorCourse_;
int averageGrade_;
Student(const Student &);
public:
Student(int enrolled_number, string major_course, int average_grade);
~Student();
const int getenrollNumber() const { return enrollNumber_; }
const int getAverageGrade() const { return averageGrade_; }
const string getMajorCourse() const { return majorCourse_;
};
}
The problem is that the IDE says that on the line class Student : Person that Person [is] not a class or struct name.
How can I create the subclass Student?

Inheritence of C++11 class

#include <string>
#include <iostream>
using namespace std;
class Surgery
{
public:
Surgery();
int getPrice();
string getType();
protected:
int price;
string type;
};
Surgery::Surgery()
{
price = 0;
type = "";
}
int Surgery::getPrice()
{
return price;
}
string Surgery::getType()
{
return type;
}
class Neurosurgery :public Surgery
{
private:
string type = "Neurosurgery";
int price = 23000;
};
class Plastic :public Surgery
{
private:
string type = "Plastic";
int price = 15000;
};
class Trauma :public Surgery
{
private:
string type = "Trauma";
int price = 5000;
};
class Endocrine :public Surgery
{
private:
string type = "Endocrine";
int price = 20000;
};
class Ophthalmological :public Surgery
{
public:
Ophthalmological();
private:
string type;
int price;
};
Ophthalmological::Ophthalmological():Surgery()
{
type = "Ophthalmological";
price = 10000;
}
int main()
{
Ophthalmological var1;
cout << var1.getPrice() << endl;
return 0;
}
When i run this code i expected to see 10000
Instead i see 0
I made it really simple to avoid any mistake with const, singlone default constructors.
First Surgery constructor gets executed after Neurosurgery.
Neurosurgery constructor should overwrite values that default Surgery constructor made.
Am i using c++11 in wrong style
This is because you are declaring tow times the variable price and type and, and when you are invoking cout << var1.getPrice() << endl; it takes the variable of Surgery. You should do:
class Surgery
{
public:
Surgery();
int getPrice();
string getType();
protected:
int price;
string type;
};
class Ophthalmological :public Surgery
{
public:
Ophthalmological();
private:
//string type; //It has been declared into Survey
//int price; //It has been declared into Survey
};
I ran your code with this modification and return the value of the unique price variable.
This is caused by the fact that it is not virtual, and there are multiple variables with the same name. So you get value from the base class Surgery. The other classes also define the variables with the same name. I think the simplest solution is this: Keep the protected variables in the base class and remove those variables from the subclasses.

Void pointer to class error

class Hero
{
public:
Hero();
virtual int useAbility(){}
virtual int basicAttack(){}
int getHitPoints();
int getManaPoints();
void setHitPoints(int x);
void setManaPoints(int x);
protected:
unsigned int hitPoints;
unsigned int manaPoints;
private:
friend void printHero();
};
class Mage : public Hero
{
public:
Mage();
int useAbility();
int basicAttack();
std::string getClassName();
protected:
private:
std::string className;
};
Same for Warrior
int input;
void *actor;
cin >> input;
if(input == 1) actor = new Warrior;
if(input == 2) actor = new Mage;
printHero(actor.getClassName(),actor.getHitPoints(),actor.getManaPoints());
So i declare the pointer 'actor' , and I want it to become a pointer to a class, but apparently it does not work.
I get this error
request for member 'getClassName' in 'actor', which is of non-class type 'void*'
Warrior and Mage have the parent class person.
you want to do:
person *actor;

Can we call member function only in member initialization list in c++?

I want provide a class with a member function that will initialize the all member of class separately.
e.g.
#include <iostream>
using namespace std;
int x = 10;
class my{
public:
my():init{}
int &i;
void init()
{
i = x;
}
};
int main()
{
my m;
return 0;
}
I know if I can use "class my : i(init())" will work, but I have some special purpose to intialize like above.
However in above example, I'm getting following error:
class ‘my’ does not have any field named ‘initMy’.
How to resolve this?
If you are trying to write a constructor for class my, then it must be named with the class name. The following will work assuming that initMy is the name of another class that you are trying to subclass.
class my : initMy
{
public:
int i;
my() {
i = 10;
}
};
You might try to pre-initialize all the fields, then calling the initializing function inside the constructor:
class my {
public:
int i;
void initMy() {
i = 10;
}
my() : i(0) { initMy(); };
};
You could also (in C++11) define a bizarre signature for a private constructor, and delegate a constructor to it
class my {
private:
void initMy () { i=10; };
enum privateen {privatev};
my(enum privateen) : i(0) { initMy(); };
public:
my() : my(privatev) {};
int i;
};
Actually, I believe that your initialization should be in a constructor, not in some other function.
Few things to clarify here.
Member initialization list is for initialize members (mostly same purpose of the constructor).In initialize list nothing to do with member functions. in this example age(newAge) is not a function. It is initializing age variable.
class Man{
private:
int age;
string name;
public:
Man(int newAge):age(newAge),name("Jhon"){}
};`
You can use constructor to initialize the members of the class.
class Man{
private:
int age;
string name;
public:
Man(int newAge)
{
age = newAge;
name = "Jhone";
}
};
Alternatively you can use a init method to do initialization, if you have some issue to use constructor.
class Man{
private:
int age;
string name;
public:
Man(){}
init(int newAge, string newName)
{
age = newAge;
name = newName;
}
};
If you need to set the value of only one member in a class, you have to use a setter method
class Man{
private:
int age;
string name;
public:
Man(){}
setAge(newAge)
{
age = newAge;
}
setName(newName)
{
name = newNAme
}
};
edit:
class Man{
private:
int age;
string name;
public:
Man(initAge, initName)
{
setValues(initAge, initName);
}
setValues(newAge, newName)
{
age = newAge;
name = newName;
}
};
int main()
{
Man goodMan(34,"Jhon");
goodMan.setValues(45,"Kevin");
}

how to do setter and getter functions for a pointer class variable?

let say i have a student class with a program class pointer for programEnrolled? how to i do the getter and setter and how do i access the member inside the programEnrolled (programName, programFees) through programEnrolled?
and when should i use a pointer function?
class clsStudent
{
private:
string studentName;
string studentID;
clsProgram *programEnrolled;
};
class clsProgram{
private:
string programName;
double programFees;
string programCode;
};
Why you need pointers in your program at all? By the way here is an example:
class clsStudent
{
public:
void setProgram(clsProgram *x) { programEnrolled=x; }
clsProgram *getProgram() const { return programEnrolled; }
...
};
clsStudent student;
student.getProgram()->programName;