Need some guidance with a c++ constructor
I have a class Student with name,surname and id number and class Teacher with name, surname and suject. I have to make class School which contains an array of Students and an array of Teachers.I have to make default constructor, copy constuctor, operator= and destructor.Also i have to make functions for finding the name of a student by a id number, function which prints the name and the surname of a student and a fuction which prints the name of the teachers, teaching a subject. What should I write in my main function? Thank you
Code:
#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
char *firstName;
char *lastName;
char *midName;
int age;
int number;
void copy(Student &other)
{
setFirstName(other.firstName);
setMidName(other.midName);
setLastName(other.lastName);
age = other.age;
number = other.number;
}
void destroy()
{
delete[] firstName;
firstName = NULL;
delete[] lastName;
lastName = NULL;
delete[] midName;
midName = NULL;
}
void setString(char *dest, const char *source) {
delete[] dest;
int sourceLen = strlen(source) + 1;
dest = new char[sourceLen];
strncpy(dest, source, sourceLen);
}
public:
Student(const char* _firstName , const char * _lastName ,const char * _midName, int _age , int _number )
{
this->firstName = NULL;
this -> midName = NULL;
this->lastName = NULL;
setFirstName(firstName);
setMidName(midName);
setLastName(lastName);
this->age = age;
this->number = number;
}
Student(Student &other)
{
firstName = NULL;
midName = NULL;
lastName = NULL;
copy(other);
}
Student& operator=(Student &other)
{
if (this != &other)
{
destroy();
copy(other);
}
return *this;
}
~Student()
{
destroy();
}
const char * getFirstName()
{
return firstName;
}
const char * getMidName()
{
return midName;
}
const char * getLastName()
{
return lastName;
}
int getAge()
{
return age;
}
int getNumber()
{
return number;
}
void setFirstName(const char *firstName)
{
setString(this->firstName, firstName);
}
void setMidName(const char *midName)
{
setString(this->midName,midName );
}
void setLastName(const char *lastName)
{
setString(this->lastName, lastName);
}
void setAge(int age)
{
this->age = age;
}
void setNumber(int number) {
this->number = number;
}
};
class Teacher
{
char *firstName;
char *midName;
char *lastName;
char subject;
void copy(Teacher &other)
{
setFirstName(other.firstName);
setMidName(other.midName);
setLastName(other.lastName);
subject = subject;
}
void destroy()
{
delete[] firstName;
firstName = NULL;
delete[] lastName;
lastName = NULL;
delete[] midName;
midName = NULL;
}
void setString(char *dest, const char *source) {
delete[] dest;
int sourceLen = strlen(source) + 1;
dest = new char[sourceLen];
strncpy(dest, source, sourceLen);
}
public:
Teacher(const char* _firstName, const char * _lastName, const char * _midName,char _subject)
{
this->firstName = NULL;
this->midName = NULL;
this->lastName = NULL;
setFirstName(firstName);
setMidName(midName);
setLastName(lastName);
}
Teacher (Teacher &other)
{
firstName = NULL;
midName = NULL;
lastName = NULL;
copy(other);
}
Teacher& operator=(Teacher &other)
{
if (this != &other)
{
destroy();
copy(other);
}
return *this;
}
~Teacher()
{
destroy();
}
const char * getFirstName()
{
return firstName;
}
const char * getMidName()
{
return midName;
}
const char * getLastName()
{
return lastName;
}
const char getSubject()
{
return subject;
}
void setFirstName(const char *firstName)
{
setString(this->firstName, firstName);
}
void setMidName(const char *midName)
{
setString(this->midName, midName);
}
void setLastName(const char *lastName)
{
setString(this->lastName, lastName);
}
void setSubject(char subject)
{
this->subject = subject;
}
};
class School
{
Student students[5];
Teacher teachers[2];
public:
};
int main()
{
system("pause");
return 0;
Re: Need some guidance with a c++ constructor
Quote:
Originally Posted by
shad0wblaz3
... What should I write in my main function? Thank you
At least, some code to test the features/functions you have implemented in your classes.
BTW, is there some reason to NOT use STL classes (std::string, std::vector, ...) rather than the plain arrays in your code? :confused:
Re: Need some guidance with a c++ constructor
In my assignment i can not use vectors
Re: Need some guidance with a c++ constructor
It never fails to amaze me that for courses that are supposed to teach c++, the c++ method of doing things either isn't taught or are not allowed! Not using string and not using vector is not the c++ way of coding! You are being taught bad c++ practices! Having explicit dynamic memory used for char strings is just not what you would use.
Also, much of the code for student/teacher is replicated. You should be considering having a base class of say Person and then have derived classes Student and Teacher from this base class.
Were you given the Student/Teacher classes as part of the assignment - or have you developed these?
Before you start, I would suggest you design the class School - what are the private elements, what are going to be the public elements? How are you going to handle search within the classes?
Once you have the class design, then implement in stages - first just the constructor/destructor then test this, then the other constructors, then the assignment, then search etc. After adding new function to the class you test this using appropriate code in main() and only move on to the next stage once the current one is working properly.
Re: Need some guidance with a c++ constructor
Quote:
You are being taught bad c++ practices!
I agree with 2kaud that it seems like bad teaching. I'll admit that I've seen projects where STL (or parts of it) is not allowed, but in general use it as default. When it comes to teaching, I believe that the high-level abstractions (strings, vectors, smart pointers, etc) and their usage should be learnt first. That way you can write safer and simpler code from the start without picking up any bad C style habits. Later it's possible to dive deeper and learn more about the C subset.
Quote:
Also, much of the code for student/teacher is replicated. You should be considering having a base class of say Person and then have derived classes Student and Teacher from this base class.
Here I disagree. Using inheritance just to avoid replicated code is often considered a code smell. Composition can be used instead. For example, a struct FullName (that holds first, mid and last name) can be reused in both classes.
Re: Need some guidance with a c++ constructor
Quote:
Using inheritance just to avoid replicated code is often considered a code smell.
In some cases, yes - but in this case Student is-a Person and Teacher is-a Person so for OOP they can correctly be derived from a Person base class. :thumb:
Re: Need some guidance with a c++ constructor
Quote:
Originally Posted by
2kaud
In some cases, yes - but in this case Student is-a Person and Teacher is-a Person so for OOP they can correctly be derived from a Person base class. :thumb:
It's not incorrect, but I don't see why it's motivated to use such a strong coupling relationship here.
It's equally true that Student has-a full name and Teacher has-a full name, right?