Classes
Contents
Contents
#include <iostream> #include <string> using namespace std; //------------------------------------------------------------- class Person { public: string firstName ; string lastName ; int age ; public: void print() ; ~Person() { cout << "Person Destructor.\n" ; } Person(int ageP, string firstNameP, string lastNameP) { age = ageP ; firstName = firstNameP ; lastName = lastNameP ; cout << "Person Constructor with arguments.\n" ; } }; //------------------------------------------------------------- int main() { Person arrayOfObjects[5] ; return(0) ; } //------------------------------------------------------------- $ g++ arr1.cpp arr1.cpp: In function ‘int main()’: arr1.cpp:30:50: error: no matching function for call to ‘Person::Person()’ 30 | Person arrayOfObjects[5] ;Our attempt at creating the array fails because the constructor ( only one ) in the Person class is expecting 3 arguments and we have provided none. Let us add a default constructor in order to make this work.
#include <iostream> #include <string> using namespace std; //------------------------------------------------------------- class Person { public: string firstName ; string lastName ; int age ; public: void print() ; ~Person() { cout << "Person Destructor.\n" ; } Person() { age = 42 ; firstName = "Sunny" ; lastName = "Liston" ; cout << "Person constructor with no arguments." << endl ; } Person(int ageP, string firstNameP, string lastNameP) { age = ageP ; firstName = firstNameP ; lastName = lastNameP ; cout << "Person Constructor with arguments.\n" ; } }; //------------------------------------------------------------- int main() { Person arrayOfObjects[5] ; return(0) ; } //------------------------------------------------------------- $ ./a.exe Person constructor with no arguments. Person constructor with no arguments. Person constructor with no arguments. Person constructor with no arguments. Person constructor with no arguments. Person Destructor. Person Destructor. Person Destructor. Person Destructor. Person Destructor.Now we are able to create the array of objects. But what if we wanted to create an array of objects and call the constructor with arguments. Then we have to use the notation Person( arguments ... ) .
#include <iostream> #include <string> using namespace std; //------------------------------------------------------------- class Person { public: string firstName ; string lastName ; int age ; public: void print() { cout << firstName << " : " << lastName << ": " << age << endl ; } ~Person() { cout << "Person Destructor for " << firstName << " : " << lastName << endl ; } Person() { age = 42 ; firstName = "Sunny" ; lastName = "Liston" ; cout << "Person constructor with no arguments." << endl ; } Person( int ageP ) { age = ageP ; firstName = "Jack" ; lastName = "Johnson" ; cout << "Person constructor with single argument." << endl ; } Person(int ageP, string firstNameP, string lastNameP) { age = ageP ; firstName = firstNameP ; lastName = lastNameP ; cout << "Person Constructor with arguments.\n" ; } }; //------------------------------------------------------------- int main() { Person arrayOfObjects[5] = { Person(20, "Joe", "Louis") , 52 } ; for( int i1=0 ; i1 < 5 ; i1++ ) { arrayOfObjects[i1].print() ; } //for return(0) ; } $ g++ arr3.cpp ; ./a.exe Person Constructor with arguments. Person constructor with single argument. Person constructor with no arguments. Person constructor with no arguments. Person constructor with no arguments. Joe : Louis: 20 Jack : Johnson: 52 Sunny : Liston: 42 Sunny : Liston: 42 Sunny : Liston: 42 Person Destructor for Sunny : Liston Person Destructor for Sunny : Liston Person Destructor for Sunny : Liston Person Destructor for Jack : Johnson Person Destructor for Joe : Louis
#include <iostream> #include <string> using namespace std; //------------------------------------------------------------- class Person { public: string firstName ; string lastName ; int age ; public: void print() { cout << firstName << " : " << lastName << ": " << age << endl ; } ~Person() { cout << "Person Destructor for " << firstName << " : " << lastName << endl ; } Person() { age = 42 ; firstName = "Sunny" ; lastName = "Liston" ; cout << "Person constructor with no arguments." << endl ; } Person( int ageP ) { age = ageP ; firstName = "Jack" ; lastName = "Johnson" ; cout << "Person constructor with single argument." << endl ; } Person(int ageP, string firstNameP, string lastNameP) { age = ageP ; firstName = firstNameP ; lastName = lastNameP ; cout << "Person Constructor with arguments.\n" ; } }; //------------------------------------------------------------- int main() { Person* arrayOfObjects = new Person[5] ; for( int i1=0 ; i1 < 5 ; i1++ ) { arrayOfObjects[i1].print() ; } //for //Must delete the array of objects delete [] arrayOfObjects ; return(0) ; } //------------------------------------------------------------- $ g++ arr4.cpp ; ./a.exe Person constructor with no arguments. Person constructor with no arguments. Person constructor with no arguments. Person constructor with no arguments. Person constructor with no arguments. Sunny : Liston: 42 Sunny : Liston: 42 Sunny : Liston: 42 Sunny : Liston: 42 Sunny : Liston: 42 Person Destructor for Sunny : Liston Person Destructor for Sunny : Liston Person Destructor for Sunny : Liston Person Destructor for Sunny : Liston Person Destructor for Sunny : Liston
We are using the notation to create our array dynamically. Person* arrayOfObjects = new Person[5] ; This is similar to the notation: int* ptr = new int[5] ; We get an array of objects with their starting address stored at the variable: arrayOfObjects However if the class has a constructor then there is no way for us to specify that using the new notation in the above example. Person* arrayOfObjects = new Person[5](55) ; produces an error of the form: [amittal@hills array]$ g++ arr7.cpp arr7.cpp: In function ‘int main()’: arr7.cpp:66:47: error: parenthesized initializer in array new [-fpermissive] Person* arrayOfObjects = new Person[5](55) ;
#include <iostream> #include <string> using namespace std; //------------------------------------------------------------- class Person { public: string firstName ; string lastName ; int age ; public: void print() { cout << firstName << " : " << lastName << ": " << age << endl ; } ~Person() { cout << "Person Destructor for " << firstName << " : " << lastName << endl ; } Person() { age = 42 ; firstName = "Sunny" ; lastName = "Liston" ; cout << "Person constructor with no arguments." << endl ; } Person( int ageP ) { age = ageP ; firstName = "Jack" ; lastName = "Johnson" ; cout << "Person constructor with single argument." << endl ; } Person(int ageP, string firstNameP, string lastNameP) { age = ageP ; firstName = firstNameP ; lastName = lastNameP ; cout << "Person Constructor with arguments.\n" ; } }; //------------------------------------------------------------- int main() { //An array of pointers to the Person object Person* arrayOfObjects[5] ; for( int i1=0 ; i1 < 5 ; i1++ ) { arrayOfObjects[i1] = new Person(55) ; } //for for( int i1=0 ; i1 < 5 ; i1++ ) { arrayOfObjects[i1]->print() ; } //for for( int i1=0 ; i1 < 5 ; i1++ ) { delete( arrayOfObjects[i1] ) ; } //for return(0) ; }//-------------------------------------------------------------