|
-
May 15th, 2016, 05:29 AM
#1
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;
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|