指针和复制构造器c ++

时间:2016-11-09 20:46:11

标签: c++ pointers copy-constructor deep-copy

当我打印这些学生作业时,我遇到了问题。我认为问题在于Course.cpp中的复制构造函数。 在主要课程“TestCourse.cpp”中,我用课程对象course1,course2和course3填写课程对象的课程和学生信息。 course3使用复制构造函数复制course1的属性,并在输出中显示。

问题:如果你查看course1输出,它会添加第一个新学生(“新学生1”),我在复制后添加到course3,但只在第一个course1输出中添加。如果我复制course2也会这样做。我怎么能解决这个/我做错了什么?作业已经上交,但下面的知识就不会受到影响:

标头文件

$locals['companies'] = Company::orderBy('name')->get(['id','name'])->toIndexedArray();

**包含函数**的Course.cpp

 /*
 * Course.h
 *
  *HEADER FILE
 */

#ifndef COURSE_H_
#define COURSE_H_
#include <string>

namespace std {

class Course {
public:
Course(const string& CourseName, int Capacity);
Course(const Course&);
 ~Course();
 //declaration for copy constructor goes here
 string getCourseName() const;
 void addStudent(const string& Name);
 void dropStudent(const string& Name);
 string* getStudents() const;
 int getNumberOfStudents() const;
 int getCapacity();
 void Clear();
 //declaration for clear function goes here

 private:
 string CourseName;
 string* Students;
 int NumberOfStudents;
 int Capacity;
};

} /* namespace std */

#endif /* COURSE_H_ */`

**司机班**

    /*
     * Course.cpp
     *
     *  Created on: Nov 4, 2016
     *      Author: diez
     */

    #include "Course.h"
    #include <iostream>
    #include <string>
    #include <iterator>

    namespace std {

    //Course Constuctor
    Course::Course(const string& CourseName, int Capacity) {

    NumberOfStudents = 0;
    this-> CourseName = CourseName;
    this-> Capacity = Capacity;
    Students = new string[Capacity];
       }//end constructor

    //Destructor
    Course::~Course() 
    {
     delete [] Students;
    }//END Destructor

    //Getter for Course Name
    string Course::getCourseName() const
    {
    return CourseName;
       }//END get

    // Code to add Student to Course Object
    void Course::addStudent(const string& Name)
    {



    //new array w/ doubly capacity
                string* newArr;
                newArr= new string[Capacity * 2];
        if(NumberOfStudents >= Capacity)
        {

            Capacity *= 2;

        //Loop setting new array equal to old array
        for ( int i = 0; i < getNumberOfStudents(); i++)
        {
            newArr[i] = Students[i];
        }
        //Sets old array to new array's capacity and information
        Students = newArr;
    }//endif

        //adds students to array
        Students[NumberOfStudents] = Name;
        NumberOfStudents++;
    }//END ADD STUDENT

/*******************************
 * DROP STUDENT FUNCTION
 *****************************/
void Course::dropStudent(const string& Name){

    /* For Loops looks through array elements*/
    for(int i=0; i< getNumberOfStudents(); i++)
        {
        /* If element is the specified student */
            if(Students[i]==Name)
            {
                /*Set element equal to the next */
                for(int j=i; j<(getNumberOfStudents()); j++)
                {
                    Students[j]=Students[j+1];

                }
            }
        }
    NumberOfStudents--;

}// DROP STUDENT

string* Course::getStudents() const{
    return Students;
}
int Course::getNumberOfStudents() const{
    return NumberOfStudents;
}
int Course::getCapacity() {
    return Capacity;
}

/********************************
 * CLEAR FUNCTION
 ************************************/
void Course::Clear(){
    for (int i = 0; i < NumberOfStudents; i++){
        Students[i] = " ";
    }
    NumberOfStudents = 0;

}//CLEAR

/******************
*COPY CONSTRUCTOR
*****************/
Course::Course(const Course& Course){
    CourseName = Course.CourseName;
    Capacity = Course.Capacity;
    NumberOfStudents = Course.NumberOfStudents;
    Students = Course.Students;
    }
    } /* namespace std */

编辑::这是输出

//============================================================================
// Name        :
// Author      : 
// Date        : 11/04/16
// Copyright   : Your copyright notice
// Description :
/*             :Program does not include double capacity function rather, doubles the capacity in the addStudent function
 :Program implements the dropStudent function
 :Program includes a new function named clear() that removes all of the students from the course
 :Program implements the destructor and copy constructor to perform a deep copy in the class
 :A third course is created that adds five students, removes one and displays the students in the course.*/
//             :
//============================================================================
#include <iostream>
#include "Course.h"
using namespace std;

int main() {

    //instances of Course w/ name and capacity
    Course course1("Data Structures", 10);
    Course course2("Database Systems", 15);

    course1.addStudent("Peter Jones");
    course1.addStudent("Lisa Walker");
    course1.addStudent("Kevin Medara");

    course1.addStudent("Joe Sansone");
    course1.addStudent("Shari Bemis");
    course1.addStudent("Vishnu");
    course1.addStudent("Kieth Naeck");


    //course 2 adding
    course2.addStudent("Jacob Fraser");
    course2.addStudent("Bob Walker");
    course2.addStudent("Kevin Medara");
    course2.addStudent("Joe Sansone");
    course2.addStudent("Shari Bemis");
    course2.addStudent("Melissa Johnson");

    // USES COPY CONSTRUCTOR in 'Course.cpp'
    Course course3(course1);
    /*
     * ADD 5 STUDENTS TO COURSE 3
     * and force array expansion
     */

    course3.addStudent("NEW STUDENT 1");
    course3.addStudent("NEW STUDENT 2");
    course3.addStudent("NEW STUDENT 3");
    course3.addStudent("NEW STUDENT 4");
    course3.addStudent("NEW STUDENT 5");

    /**************************************************
     * COURSE 1 CONSOLE OUTPUT
     ***************************************************/

    /*               **NOTE**
     * each course output has the same properties, course3 copies the properties
     * of course1
     */
    cout << "Class: " << course1.getCourseName() << " | Number Of Students: "
            << course1.getNumberOfStudents() << "\nStudents In Course One "
            << " Capacity: " << course1.getCapacity() << endl; // prints !!!Hello World!!!

    /*pointer string array to iterate through course Students*/
    string* students = course1.getStudents();

    for (int i = 0; i <= course1.getNumberOfStudents(); i++) {

        cout << i << " " << students[i] << endl;

    }

    cout << "_____________________________________" << endl;

    /*************************************************
     * COURSE 2 CONSOLE OUTPUT
     *************************************************/
    cout << "Class: " << course2.getCourseName() << " | Number Of Students: "
            << course2.getNumberOfStudents() << "\nStudents In Course Two "
            << " Capacity: " << course2.getCapacity() << endl;
    students = course2.getStudents();
    for (int i = 0; i <= course2.getNumberOfStudents(); i++) {
        cout << i << " " << students[i] << "\n";
    }
    cout << "______________________________________" << endl;

    /*********************
     * COURSE 3 CONSOLE OUTPUT
     *deep copies console 1
     *************************************/

    cout << "Class: " << course3.getCourseName() << " | Number Of Students: "
            << course3.getNumberOfStudents() << "\nStudents In Course Three "
            << " Capacity: " << course3.getCapacity() << endl;
    students = course3.getStudents();

    for (int i = 0; i <= course3.getNumberOfStudents(); i++) {
        cout << i << " " << students[i] << "\n";
    }

    cout << "_____________________________________" << endl;

    /***********************************************************************************
     * CALL DROP STUDENT FUNCTION on course1
     * w/ print out
     *******************************************/
    course1.dropStudent("Kevin Medara");

    cout << "Course One after dropping myself\n" << "Class: "
            << course1.getCourseName() << " | Number Of Students: "
            << course1.getNumberOfStudents() << "\nStudents In Course One "
            << " Capacity: " << course1.getCapacity() << endl;
    students = course1.getStudents();

    for (int i = 0; i < course1.getNumberOfStudents(); i++) {
        cout << i << " " << students[i] << endl;
    }

    cout << "_____________________________________" << endl;

    /**********************
     * CALL CLEAR FUNCTION OF COURSE 1
     * w/ print out
     ****************************/
    course1.Clear();

    cout << "Cleared Course One\n" << "Class: " << course1.getCourseName()
            << " | Number Of Students: " << course1.getNumberOfStudents()
            << "\nStudents In Course One " << " Capacity: "
            << course1.getCapacity() << endl;

    students = course1.getStudents();

    for (int i = 0; i <= course1.getNumberOfStudents(); i++) {

        cout << i <<" " << students[i] << endl;

    }

    cout << "_____________________________________" << endl;

    /**********************************
     * DROP STUDENT FROM COURSE 3
     * w/ print
     ************************************/

    course3.dropStudent("Shari Bemis");

    cout << "Course 3 After Dropping Shari Bemis\n" << "Class: "
            << course3.getCourseName() << " | Number Of Students: "
            << course3.getNumberOfStudents() << "\nStudents In Course Three "
            << " Capacity: " << course3.getCapacity() << endl;
    students = course3.getStudents();

    for (int i = 0; i <= course3.getNumberOfStudents(); i++) {

        cout << i <<" "<< students[i] << "\n";

    }

    cout << "_____________________________________" << endl;

    return 0;
}

1 个答案:

答案 0 :(得分:1)

将复制构造函数更改为:

Course::Course(const Course& Course)
{
    CourseName = Course.CourseName;
    Capacity = Course.Capacity;
    NumberOfStudents = Course.NumberOfStudents;
    cout << &Students << endl;
    cout<< Course.Students;

    //allocate the new memory
    Students = new string[Capacity];

    //actually copy over the data, which was apparently the problem
    for(int i = 0; i < NumberOfStudents; i ++)
    {
        Students[i] = Course.Students[i];
    }

}