Classes with Arrays using other class objects - c++

I have two completed classes at the moment, the Teacher and Student classes have default definitions.
Right now I am trying to figure out the Classroom and School classes. The Classroom class is supposed to hold a Teacher and an array of 35 Student objects.
The School class is supposed to contain an array of 100 Classroom objects.
How do I do this, I sort of know how to initialize an array in a class but I'm not sure how to achieve this using the objects of another class?
class Teacher
{
private:
string last;
string first;
int gradeLevel;
public:
Teacher();
};
Teacher::Teacher()
{
last = "AAAA";
first = "BBBB";
gradeLevel = 0;
}
class Student
{
private:
string studLast;
string studFirst;
public:
Student();
};
Student::Student()
{
studLast = "AAAA";
studFirst = "BBBB";
}
class Classroom
{
};
class School
{
};

For example:
class Classroom
{
private:
Teacher t; // create a teacher
Student s[35]; // create an array of 35 students
...
};
class School
{
private:
Classroom rooms[100]; // create an array of 100 rooms
...
};

What you want to do here is create a Teacher, just one like you wanted, and then create an array of Student objects, which if you didn't know is done like Student students[35];. Then to the School object which is just an array of Classroom objects. Here is the full code:
class Classroom
{
private:
Teacher teacher;
Student students[35];
public:
Classroom();
};
Classroom::Classroom()
{
;
}
class School
{
private:
Classroom classrooms[100];
public:
School();
};
School::School()
{
;
}
Note: all of the items in the arrays are initialized when you write something like Student students[35];. You can check this by doing cout << this->stduents[12].studLast << endl;

Related

I have problem with least privilege principle. incrementing a member when an object is created

I want to keep track of the number of students in my system so, My idea was to make a static datamember in the "StudentController" class called "_numOfStudents" and increment it with the Student's constructor but it didn't work so, I moved it into the "Student" class and made that when a Student object is created the number increment by the help of the constructor. The problem is: isn't it not the Student class's business to know how many students are there thus breaking the principle of least privilege. what can I do better to keep track of the student objects' count.
Student(string firstName, string lastName, int age,vector<float>&subjects)//Student conctructor
{
SetFirstName(firstName);
setLastName(lastName);
SetAge(age);
SetMarks(subjects);
this->m_id++;
StudentController::_numberOfStudents++;
}
class StudentController//this is where i declared the static data member
{
private:
list<Student> _students;
public:
static int _numberOfStudents;
StudentController() {};
StudentController(list<Student>& st) :_students(st) {};
}
}
};
int StudentController::_numberOfStudents = 0;
When you try to do StudentController::_numberOfStudents++; in the constructor, the StudentController class is not yet defined, therefore the compiler doesn't know about that class and its static member.
Maybe keeping track of the students shouldn't be the role of the Student class itself. Instead it should be the role of a separate Classroom object:
struct Student;
struct Classroom {
void add(Student&) {
m_count++;
}
size_t count() const { return m_count; }
private:
size_t m_count{0};
};
struct Student {
Student(Classroom& classroom) {
classroom.add(*this);
}
};
int main()
{
Classroom classroom;
Student alice{classroom};
Student bob{classroom};
assert(classroom.count() == 2);
}

c++ how do you implement a has relationship?

for example:
i got great reservations:
the class has memeber the start/end date and a function that gives me the date.
class reservation{
private:
datetime start;
datetime end;
public:
reservation(stardatetime, enddatetime): start(stardatetime), end(enddatetime)
{
}
std::list<std::string> get_reservation()
{
for example: return start;
}
};
and i have a class conference room, which can be booked from one time to another.
class room{
private:
int room_ID;
public:
room(int id):room_ID(id)
{
}
void get_room(){
return room_id;
}
}
that means every room has several reservations.
how can i access through a room id via the reservation?
if i search for room id 1, the reservation of the room will also be displayed.
Thanks.
Here's your problem statement: every room has many reservations, and every reservation has a reference to a single room. Based on this, I would write it as follows (you may add your stuff inside those classes).
#include <iostream>
#include <vector>
using namespace std;
class Room; //forward declaration
class Reservation {
private:
Room* room;
public:
Reservation(Room* roomPtr) {
room = roomPtr;
}
};
class Room {
vector<Reservation> reservations;
};
int main(){
Room room[10]; //an array of 10 rooms with no reservations (calls def.constructor of each Room)
Reservation r1(&room[0]); //reservation object r1 reserving room[0]
Reservation r2(&room[1]); //reservation object r2 reserving room[1]
// ...more stuff goes here
}

Cannot inherit data from parent class to child class in C++

I am trying to inherit data from two parent classes of Employee and Student to a child class of Manager.I have created set and get functions of each class and i have created a show function in child class which will also show data of both parent classes.But when i make objects and call functions of set values and then show data, only the data of child class is shown.
Can anyone tell my why is that and how do i solve it? Thanks for any help.
Code is below:
class Employee{
protected:
string name;
int number;
public:
Employee(){
name = "";
number = 0;
}
void set_name(string a){
name = a;
}
void set_number(int a){
number = a;
}
string get_name(){
return name;
}
int get_number(){
return number;
}
};
class Student{
protected:
string school;
string degree;
public:
Student(){
school = "";
degree = "";
}
void set_school(string a){
school = a;
}
void set_degree(string a){
degree = a;
}
string get_school(){
return school;
}
string get_degree(){
return degree;
}
};
class Manager:protected Employee, protected Student{
protected:
string title;
int dues;
public:
Manager(){
title = "";
dues = 0;
}
void set_title(string a){
title = a;
}
void set_dues(int a){
dues = a;
}
string get_title(){
return title;
}
int get_dues(){
return dues;
}
void show_data(){
cout << Employee::get_name();
cout << Employee::get_number() << endl;
cout << Student::get_school() << endl;
cout << Student::get_degree() << endl;
cout << get_title() << endl;
cout << get_dues() << endl;
}
};
int main(){
Employee emp;
Student stu;
Manager man;
emp.set_name("Fahad");
emp.set_number(10);
stu.set_school("COMSAT");
stu.set_degree("BSCS");
man.set_title("Manager Title");
man.set_dues(100);
man.show_data();
return 1;
}
You have 3 different objects, each contains its own data!
For example, your manager class contains all attributes from manager, employee and student. In your main function:
Employee emp;
Student stu;
Manager man;
emp.set_name("Fahad");
emp.set_number(10);
stu.set_school("COMSAT");
stu.set_degree("BSCS");
man.set_title("Manager Title");
man.set_dues(100);
man.show_data();
you only set some of the attributes of your objects. For manager, you only set title and dues. If you want to set also the name, you have to do it!
BTW: there is no need to use Student::get_school() to access the members of parent classes if you have not used the same attribute name multiple times or inherit multiple times from the same class.
I believe you want to do the following:
class Manager:public Employee, public Student{ ... };
int main(){
Manager man;
man.set_name("Fahad");
man.set_number(10);
man.set_school("COMSAT");
man.set_degree("BSCS");
man.set_title("Manager Title");
man.set_dues(100);
man.show_data();
return 1;
}
If you change to public for deriving from your parent classes, you can directly access the getter/setter functions from the object as you can see. As you have already getter and setter functions, it is a good idea to make the member vars now private.
BTW: The return value of main is typically 0 to tell that there is no error. If you want to return with "no error", you can simply omit the return statement. main() will than return 0 by default.
The following lines create three objects.
Employee emp;
Student stu;
Manager man;
The emp object is not related to the Employee sub-object of man and the stu object is not related to the Student sub-object of man.
The lines
emp.set_name("Fahad");
emp.set_number(10);
change the state of the independent emp object. They don't change the state of the Employee sub-object of man.
You need to use:
int main(){
// Employee emp; Not needed
// Student stu; Not needed
Manager man;
man.set_name("Fahad");
man.set_number(10);
man.set_school("COMSAT");
man.set_degree("BSCS");
man.set_title("Manager Title");
man.set_dues(100);
man.show_data();
return 1;
}
to see the values set on the same object. However, to do that you need to change the inheritances of Manager. Use
class Manager : public Employee, public Student {
...
};
Your code does not work as expected because:
You allocate separate objects/instances of each Employee, Student and Manager, e.g. emp, stud and man and set the data on each separate instance (for example, the emp.set_name() will only affect the emp object, not the man object).
You print the data member of the man object which does not include the data set in emp and man.
I do not know what the ideas behind your code design is, I do recommend to leave out multiple inheritance as it quickly leads to unmaintainable code. But to get your example code to work, do the following:
Delete the allocation of emp and stu;
change the emp and stu variable names in the setter calls to man.
Change protected to public in the Manager declaration where Manager extends Student and Employee.
class Manager:public Employee, public Student{
int main(){
Manager man;
man.set_name("Fahad");
man.set_number(10);
man.set_school("COMSAT");
man.set_degree("BSCS");
man.set_title("Manager Title");
man.set_dues(100);
man.show_data();
return 1;
}
And you will get the output I assume you expect.

Creating an object that is part of multiple classes

I am trying to build a program where I wanted to create an object that is part of two classes.
I have the class Student and then I have a class for Node. And I want to create an object that is at the same time a Student and a Node. I tried doing this:
Student James;
James = new Node;
But that doesn't work.
Any help on how to do this? Thanks.
Use inheritance: http://en.wikipedia.org/wiki/Inheritance_%28computer_science%29
class Node {
//....
}
class Student : public Node {
//....
}
Student James;
You can treat object James as Node via pointer to base object:
Node * p = &James;
What you're looking for is multiple inheritance:
class MyClass : public Student, public Node
{
// ...
};
MyClass James;
You may want to read up on this feature of C++, it's not without its pitfalls.
EDIT
The question here is: what is the relation between Student and Node?
If Student is a kind of Node (e.g. you have students, teachers, etc and you all want these to behave as nodes), then you can simply inherit Student from Node:
class Student : public Node
{
// ...
};
Student James;
In this case, you can also do:
Node* James = new Student();
// do stuff with James
delete James;
If Student and Node are independent, you have to ask yourself if you really want an object that can behave as both at the same time. Perhaps you only need an object that pairs a Student and a Node:
struct MyStruct
{
Student myStudent;
Node myNode;
};
MyStruct James;
If you actually need an object that can behave as both Student and Node (even though those two things are independent) then you'll need multiple inheritance.
Use polymorphism:
Class Base
{
}
class Student : public Base
{
}
class Node : public Base
{
}
Now:
Student s;
Node n;
Base* b = &s;
b = &n;

QList pointer function to abstract class

I feel dumb for asking this question because it seems like it would be simple but I don't know how to do it and I can't find it anywhere on the internet. I'm trying to make a function that will return a QList to standard output, the pointer to the abstract class is confusing me. The AbstractStudent class generates an instance of another class Student. here is the function:
QList<AbstractStudent*>* StudentList::returnList() const{
}
A list which stores pointers of an abstract class will be able to store pointers to any sub class of that abstract class.
Think of the following:
AbstractStudent.h:
class AbstractStudent
{
// ...
};
Student.h:
class Student : public AbstractStudent
{
// ...
};
Any other class .cpp:
QList< AbstractStudent* > studentList;
// Each of the following works:
AbstractStudent* student1 = new Student( /* ... */ );
studentList.append( student1 );
Student* student2 = new Student( /* ... */ );
studentList.append( student2 );
Student* student3 = new Student( /* ... */ );
AbstractStudent* student3_1 = student3;
studentList.append( student3 );
I am however a bit confused of your last sentence, claiming that AbstractStudent generates Student objects. I would have expected that Student inherits AbstractStudent and some other class generates Student objects, like in my example.