1 / 11

Classes Class Data Members & Initializers

Department of Computer and Information Science, School of Science, IUPUI. Classes Class Data Members & Initializers. CSCI 240. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. Class Objects as Members -- Example.

vachel
Download Presentation

Classes Class Data Members & Initializers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Department of Computer and Information Science,School of Science, IUPUI ClassesClass Data Members & Initializers CSCI 240 Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

  2. Class Objects as Members -- Example • class student{ char *name; public: student(char *ip) { name = new char[50]; strcpy(name, ip); } ~student(){delete[] name;} };

  3. Class Objects as Members -- Example • class instructor{ char *name; public: instructor(char *ip) { name = new char[50]; strcpy(name, ip); } ~instructor(){delete[] name;} };

  4. Class Objects as Members -- Example • class csci_c{ student s1, s2; instructor i1; char *location; public: //Constructor Initializer csci_c(char *n1, char *n2, char *n3, char *l) :s1(n1), s2(n2), i1(n3){ location = new char[50]; strcpy(location, l); } ~csci_c(){delete[] location;} }; • main() { csci_c fall_class("John","Tom","Henry","SI102");}

  5. Class Objects as Data Members • Data Members can be User Defined Types Having their Constructors • Constructor Initializer is Used • Appropriate Constructors are Invoked on the Order they are Specified in the Class Declaration • Destructor of the Class Containing Members of Other Classes is Called First, Followed by the Member's Destructors in Reverse Order of Declaration

  6. Constructor Initializer •   Specifies the Processing to be Performed Before the Actual Constructor Processing Starts • A List of Entries, Each of Which has: • Identifier of Fundamental Data Type • Identifier of a Class Object • Name of a Base Class -- Inheritance • Each Name is Followed by Its Initialization Values in Parenthesis • Appropriate Constructors are Invoked

  7. Constructor Initializer -- Example • /* Assume the Previous Definitions of Classes "student" and "instructor" are Valid */ class csci_c{ student s1, s2; instructor i1; char *location; int capacity; public: /* Constructor Initializer */ csci_c(char *n1,char *n2,char *n3,char *l,int c) :s1(n1), s2(n2), i1(n3), location(l), capacity(c){} ~csci_c(){} }; • main() { csci_c summer("John","Tom","Henry","SL110",20); }

  8. Array of Class Objects -- A Class With a Constructor • Default Constructor is MUST • All the Objects will be Initialized as per the Default Constructor • You can use array initializers to pass a single argument to a constructor of a declared array • There is NO Way to pass an argument to a constructors of a Dynamic Array Declaration • Destructor MUST be Called for EACH ELEMENT in the Array • Automatic Array Objects -- Implicit Destruction • Explicitly Created Objects by new -- delete[] Operator

  9. Array of Objects -- Example • #define size 50 • class student{ char *name; public: student() //Default Constructor {name = new char[size]; strcpy(name, "No Name");} student (char *ip): name(ip){} ~student(){delete[] name;} };

  10. Array of Objects -- Example • main(){ /* object array of "student" class all initialized to "No Name" – default constructor */ student s1[size]; /* Initialization -- second constructor */ student s2[2] = {"John", "Tom"}; /* Dynamically created array -- default constructor */ student *s3 = new student[3]; /* Dynamically created object -- default constructor */ student *s4 = new student; /* "s1" and "s2" arrays will be automatically destroyed when "main“ exits. However, array pointed by "s3" and object pointed by "s4", must be deleted. */ delete[] s3; delete s4;}

  11. Acknowledgements • These slides were originally prepared by Rajeev Raje, modified by Dale Roberts.

More Related