CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2016
    Posts
    2

    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;

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Need some guidance with a c++ constructor

    Quote Originally Posted by shad0wblaz3 View Post
    ... 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?
    Victor Nijegorodov

  3. #3
    Join Date
    May 2016
    Posts
    2

    Re: Need some guidance with a c++ constructor

    In my assignment i can not use vectors

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Aug 2006
    Posts
    231

    Re: Need some guidance with a c++ constructor

    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.

    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.
    Last edited by TubularX; May 15th, 2016 at 05:49 PM.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Need some guidance with a c++ constructor

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Aug 2006
    Posts
    231

    Re: Need some guidance with a c++ constructor

    Quote Originally Posted by 2kaud View Post
    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.
    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?

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
  •  





Click Here to Expand Forum to Full Width

Featured