Classes Class Data Members & Initializers

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Classes Separating interface from implementation
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
Dale Roberts Introduction to Java - Access Specifiers Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Dale Roberts Object Oriented Programming using Java - Enumerations Dale Roberts, Lecturer Computer Science, IUPUI Department.
Dale Roberts 1 Classes Constructors & Destructors Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer.
Copy Constructors Fall 2008 Dr. David A. Gaitros
Dale Roberts Object Oriented Programming using Java - OOD to OOP: ATM Case Study Dale Roberts, Lecturer Computer Science, IUPUI
More C++ Features True object initialisation
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Information Representation: Negative Integer Representation.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer Data Structure.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Dale Roberts 1 Operator Overloading Member Functions Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
Learners Support Publications Constructors and Destructors.
LThe C++ programming language Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./0. lExample on definition of an object having.
Object Oriented Programming using Java - Composition
Constructors and Destructors
GUI Programming using Java - Key Events
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Characters and Strings
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Abstract Data Types Polynomials CSCI 240
CISC181 Introduction to Computer Science Dr
Object Oriented Programming using Java - Class Instance Variables
Variable Declarations, Data types, Expressions
Variable Declarations, Data types, Expressions
Abstract Data Types Sparse Matrices CSCI 240
Constructor & Destructor
group work #hifiTeam
Scope, Parameter Passing, Storage Specifiers
Functions Declarations CSCI 230
Classes Access Specifiers
Advanced Programming Basics
Negative Integer Representation
Contents Introduction to Constructor Characteristics of Constructor
Dale Roberts, Lecturer IUPUI
Functions Recursion CSCI 230
Classes Copy Constructors
Department of Computer and Information Science, School of Science, IUPUI CSCI 265 Classes Dale Roberts, Lecturer Computer Science, IUPUI
Pointers Call-by-Reference CSCI 230
Constructors and destructors
Constructors and Destructors
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Classes Static Members
CISC181 Introduction to Computer Science Dr
Destructor CSCE 121 J. Michael Moore.
CS148 Introduction to Programming II
The C++ programming language
9-10 Classes: A Deeper Look.
Destructor CSCE 121.
CS410 – Software Engineering Lecture #5: C++ Basics III
A Deeper Look at Classes
Characters and Strings Functions
Characters and Strings
Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI
Classes Introduction CSCI 240
CS148 Introduction to Programming II
Constructors & Destructors
9-10 Classes: A Deeper Look.
Abstract Data Types Stacks CSCI 240
Classes Member Qualifiers
Presentation transcript:

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

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

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

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");} 8/29/2019

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 8/29/2019

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 8/29/2019

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/29/2019

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 8/29/2019

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;} }; 8/29/2019

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; } 8/29/2019

Acknowledgements These slides were originally prepared by Rajeev Raje, modified by Dale Roberts. 8/29/2019