[英]Pointers and Copy Constructors c++
打印這些學生陣列以進行作業時遇到問題。 我認為問題出在Course.cpp中的副本構造函數中。 在主類“ TestCourse.cpp”中,我用課程和課程對象Course1,Course2和Course3填充課程和學生信息。 course3使用復制構造函數復制course1的屬性,該屬性將顯示在輸出中。
問題:如果您查看Course1的輸出,它將添加第一個新學生(“ New Student 1”),我在復制后將其添加到Course3,但僅在第一個Course1輸出中。 如果我也復制course2,它將執行此操作。 我該如何解決/我在做什么錯? 作業已經上交,但知道下面的代碼就不會感到痛苦:
頭文件
/*
* 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包含函數**
/*
* 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;
}
編輯::這是輸出
Class: Data Structures | Number Of Students: 7
Students In Course One Capacity: 10
0 Peter Jones
1 Lisa Walker
2 Kevin Medara
3 Joe Sansone
4 Shari Bemis
5 Vishnu
6 Kieth Naeck
7 NEW STUDENT 1
_____________________________________
Class: Database Systems | Number Of Students: 6
Students In Course Two Capacity: 15
0 Jacob Fraser
1 Bob Walker
2 Kevin Medara
3 Joe Sansone
4 Shari Bemis
5 Melissa Johnson
6
______________________________________
Class: Data Structures | Number Of Students: 12
Students In Course Three Capacity: 20
0 Peter Jones
1 Lisa Walker
2 Kevin Medara
3 Joe Sansone
4 Shari Bemis
5 Vishnu
6 Kieth Naeck
7 NEW STUDENT 1
8 NEW STUDENT 2
9 NEW STUDENT 3
10 NEW STUDENT 4
11 NEW STUDENT 5
12
_____________________________________
Course One after dropping myself
Class: Data Structures | Number Of Students: 6
Students In Course One Capacity: 10
0 Peter Jones
1 Lisa Walker
2 Joe Sansone
3 Shari Bemis
4 Vishnu
5 Kieth Naeck
_____________________________________
Cleared Course One
Class: Data Structures | Number Of Students: 0
Students In Course One Capacity: 10
0
_____________________________________
Course 3 After Dropping Shari Bemis
Class: Data Structures | Number Of Students: 11
Students In Course Three Capacity: 20
0 Peter Jones
1 Lisa Walker
2 Kevin Medara
3 Joe Sansone
4 Vishnu
5 Kieth Naeck
6 NEW STUDENT 1
7 NEW STUDENT 2
8 NEW STUDENT 3
9 NEW STUDENT 4
10 NEW STUDENT 5
11
_____________________________________
將復制構造函數更改為:
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];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.