#include <string> #include <iostream> using std::cin ; using std::cout ; using std::flush ; using std::endl ; using std::string ; typedef string * strPtr ; [FONT="]class[/FONT] [FONT="]Student[/FONT] { public: [FONT="]Student[/FONT] ( ) ; //default ctor [FONT="]Student[/FONT] ( string sName , int numC , string cList[ ] ) ; //parameterized ctor [FONT="]Student[/FONT] ( [FONT="]Student[/FONT] & cpy ) ; //copy ctor ~Student ( ) ; //dtor [FONT="]Student[/FONT] operator = ( const [FONT="]Student[/FONT] & rtSide ) ; void input ( ) ; //input data from user void output ( ) ; //output data void resData ( ) ; //reset numClass and [FONT="]classList[/FONT][] private: int numClass ; string [FONT="]name[/FONT] ; strPtr [FONT="]classList[/FONT] ; } ; #include "prob4.h" int main ( ) { string s2Classes[] = { "Psychology" , "C++" , "Government" , "NV.NET" , "C#" } ; [FONT="]Student[/FONT] s1 ; [FONT="]Student[/FONT] s2 ( "Lilly" , 5 , s2Classes ) ; s1.input ( ) ; s1.output ( ) ; s2.output ( ) ; return 0 ; } //File: prob4.func //header file #include "prob4.h" [FONT="]Student[/FONT]::[FONT="]Student[/FONT] ( ) : numClass ( 4 ) , [FONT="]name[/FONT] ( "John Doe" ) , [FONT="]classList[/FONT] ( NULL ) { /*Body intentionally empty*/ } [FONT="]Student[/FONT]::[FONT="]Student[/FONT] ( string sName , int numC , string cList[ ] ) : [FONT="]name[/FONT] ( sName ) , numClass ( numC ) { [FONT="]classList[/FONT] = new string [ ] ; for ( int i = 0 ; i < numClass ; i++ ) [FONT="]classList[/FONT][ i ] = cList[ i ] ; } [FONT="]Student[/FONT]::[FONT="]Student[/FONT] ( [FONT="]Student[/FONT] & cpy ) : [FONT="]name[/FONT] ( cpy.[FONT="]name[/FONT] ) , numClass ( cpy.numClass ) { [FONT="]classList[/FONT] = new string[ numClass ] ; for ( int i = 0 ; i < numClass ; i++ ) [FONT="]classList[/FONT][ i ] = cpy.[FONT="]classList[/FONT][ i ] ; } [FONT="]Student[/FONT]::~Student() { delete [ ] [FONT="]classList[/FONT] ; } [FONT="]Student[/FONT] [FONT="]Student[/FONT]:perator =(const [FONT="]Student[/FONT] &rtSide) { if ( [FONT="]name[/FONT] == rtSide.[FONT="]name[/FONT] ) return *this ; numClass = rtSide.numClass ; delete [] [FONT="]classList[/FONT] ; [FONT="]classList[/FONT] = new string [] ; for ( int i = 0 ; i < numClass ; i++ ) [FONT="]classList[/FONT] = rtSide.[FONT="]classList[/FONT] ; return *this ; } void [FONT="]Student[/FONT]::input ( ) { cout << "This program reads a [FONT="]student[/FONT] [FONT="]name[/FONT] and the number and [FONT="]name[/FONT] of the classes.\n" ; cout << "Enter the [FONT="]student[/FONT] [FONT="]name[/FONT]: " << flush ; getline ( cin , [FONT="]name[/FONT] , '\n' ) ; cout << "Enter the number of classes: " << flush ; cin >> numClass ; cout << "Enter " << numClass << " classes, each followed by pressing 'Enter':\n" ; for ( int i = 0 ; i < numClass ; i++ ) getline( cin , [FONT="]classList[/FONT] [ i ] ) ; } void [FONT="]Student[/FONT]::resData( ) { numClass = 0 ; [FONT="]classList[/FONT] = NULL ; } void [FONT="]Student[/FONT]:utput() { cout << "Outputting [FONT="]student[/FONT] data for: " << [FONT="]name[/FONT] << endl ; for ( int i = 0 ; i < numClass ; i++ ) { cout << "\t" << i+1 << ".\t" << [FONT="]classList[/FONT][ i ] << endl ; } }
Please post you code, (1) Properly indented (2) Always within [noparse] Code: ...<your code>... [/noparse]. I have done it for you this time, but next time, your thread might be closed/removed. Code: #include <string> #include <iostream> using std::cin; using std::cout ; using std::flush ; using std::endl ; using std::string ; typedef string * strPtr ; class Student { public: Student ( ) ; //default ctor Student ( string sName , int numC , string cList[ ] ) ; //parameterized ctor Student ( Student & cpy ) ; //copy ctor ~Student ( ) ; //dtor Student operator = ( const Student & rtSide ) ; void input ( ) ; //input data from user void output ( ) ; //output data void resData ( ) ; //reset numClass and classList[] private: int numClass ; string name ; strPtr classList ; }; #include "prob4.h" int main ( ) { string s2Classes[] = { "Psychology" , "C++" , "Government" , "NV.NET" , "C#" } ; Student s1 ; Student s2 ( "Lilly" , 5 , s2Classes ) ; s1.input ( ) ; s1.output ( ) ; s2.output ( ) ; return 0 ; } File: prob4.func Code: //header file #include "prob4.h" Student::Student ( ) : numClass ( 4 ) , name ( "John Doe" ) , classList ( NULL ) { /*Body intentionally empty*/ } Student::Student ( string sName , int numC , string cList[ ] ) : name ( sName ) , numClass ( numC ) { classList = new string []; for ( int i = 0 ; i < numClass ; i++ ) classList[ i ] = cList[ i ] ; } Student::Student ( Student & cpy ) : name ( cpy.name ) , numClass ( cpy.numClass ) { classList = new string[ numClass ] ; for ( int i = 0 ; i < numClass ; i++ ) classList[ i ] = cpy.classList[ i ] ; } Student::~Student() { delete [ ] classList ; } Student Student::operator =(const Student &rtSide) { if ( name == rtSide.name ) return *this ; numClass = rtSide.numClass ; delete [] classList ; classList = new string [] ; for ( int i = 0 ; i < numClass ; i++ ) classList = rtSide.classList ; return *this ; } void Student::input ( ) { cout << "This program reads a student name and the number and name of the classes.\n" ; cout << "Enter the student name: " << flush ; getline ( cin , name , '\n' ) ; cout << "Enter the number of classes: " << flush ; cin >> numClass ; cout << "Enter " << numClass << " classes, each followed by pressing 'Enter':\n" ; for ( int i = 0 ; i < numClass ; i++ ) getline( cin , classList [ i ] ) ; } void Student::resData( ) { numClass = 0 ; classList = NULL ; } void Student::output() { cout << "Outputting student data for: " << name << endl ; for ( int i = 0 ; i < numClass ; i++ ) { cout << "\t" << i+1 << ".\t" << classList[ i ] << endl ; } } You should also mention something abt what exactly you want us to do with your code, instead of just dumping it here. BTW, your code doesn't compile since you have declarations like : Code: classList = new string []; You need to mention the size inside [].