DOC PREVIEW
IUPUI CS 265 - Classes

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Classes Part 2Friend Function -- Example Slide 3Friend Classes -- Danger!?Friend Class -- ExampleSlide 6Class Objects as MembersClass Objects as Members -- ExampleSlide 9Slide 10Constructor InitializerConstructor Initializer -- ExampleArray of Class Objects -- A Class With a ConstructorArray of Objects -- ExampleSlide 15Self-Reference -- this this – Example 1this -- Example 2this -- Example 2 ….Pointers to Members Pointers to Data Members -- ExamplePointers to Member Functions -- ExampleMember Functions -- Qualifiers inline Member Functions -- ExampleNested Classes Nested Classes -- ExampleSlide 27Constant Data MembersConstant Member FunctionsConstant Members and Functionsconst -- Example Acknowledgements01/14/19 1Dale RobertsClasses Part 2Department of Computer and Information Science,School of Science, IUPUIDale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected] 26501/14/19 2Dale RobertsFriend Function -- Example Friend Function -- Example #define size 50 #define size 50 class student{ class student{ char *name; char *name; public: public: student(char *ip) student(char *ip) {name = new char[size]; strcpy(name, {name = new char[size]; strcpy(name, ip);}ip);} print() {cout << "Name: " << name print() {cout << "Name: " << name << endl;} << endl;} //friend function//friend functionfriend void dangerous_fn(student&); friend void dangerous_fn(student&); }; };01/14/19 3Dale RobertsFriend Function -- Example Friend Function -- Example void dangerous_fn(student &s) void dangerous_fn(student &s) {strcpy(s.name, "DANGER");} {strcpy(s.name, "DANGER");} main(){ main(){ student s1("John"); student s1("John"); s1.print(); s1.print(); dangerous_fn(s1); dangerous_fn(s1); s1.print();} s1.print();} OUTPUT WILL BE: OUTPUT WILL BE: Name: JohnName: JohnName: DANGER Name: DANGER01/14/19 4Dale RobertsFriend Classes -- Danger!?Friend Classes -- Danger!?Not Directly Associated with the Class Not Directly Associated with the Class All the Member Functions of the Friend Class All the Member Functions of the Friend Class Can Access Non-Public Data Members of the Can Access Non-Public Data Members of the Original Class Original Class Can be Friend of More than One Classes Can be Friend of More than One Classes Violation of Encapsulation Violation of Encapsulation SHOULD BE USED ONLY WHEN SHOULD BE USED ONLY WHEN REQUIRED REQUIRED01/14/19 5Dale RobertsFriend Class -- ExampleFriend Class -- Example#define size 50 #define size 50 class student{ class student{ char *name; char *name; public: public: student(char *ip) student(char *ip) {name = new char[size]; strcpy(name, {name = new char[size]; strcpy(name, ip);} ip);} print() {cout << "Name: " << name << print() {cout << "Name: " << name << endl;} endl;} //friend class //friend class friend class dummy; friend class dummy; }; };01/14/19 6Dale RobertsFriend Class -- ExampleFriend Class -- Exampleclass dummy{ //Friend Class class dummy{ //Friend Class public: public: void danger_member_fn(student &s) void danger_member_fn(student &s) {strcpy(s.name, "FRIEND or FOE?");}{strcpy(s.name, "FRIEND or FOE?");}}; }; main(){main(){ student s("John"); s.print(); student s("John"); s.print(); dummy risk; dummy risk; risk.danger_member_fn(s); s.print();} risk.danger_member_fn(s); s.print();} OUTPUT WILL BE: OUTPUT WILL BE: Name: John Name: John Name: FRIEND or FOE? Name: FRIEND or FOE?01/14/19 7Dale RobertsClass Objects as MembersClass Objects as MembersData Members are of User Defined Types Having Data Members are of User Defined Types Having their Constructors their Constructors Constructor Initializer is Used Constructor Initializer is Used Appropriate Constructors are Invoked on the Appropriate Constructors are Invoked on the Order they are Specified in the Class Order they are Specified in the Class Declaration Declaration Destructor of the Class Containing Members of Destructor of the Class Containing Members of Other Classes is Called First, Followed by the Other Classes is Called First, Followed by the Member's Destructors in Reverse Order of Member's Destructors in Reverse Order of Declaration Declaration01/14/19 8Dale RobertsClass Objects as Members -- ExampleClass Objects as Members -- Exampleclass student{ class student{ char *name; char *name; public: public: student(char *ip) { student(char *ip) { name = new char[50]; name = new char[50]; strcpy(name, ip); strcpy(name, ip);}}~student(){delete[] name;} ~student(){delete[] name;} }; };01/14/19 9Dale RobertsClass Objects as Members -- ExampleClass Objects as Members -- Exampleclass instructor{ class instructor{ char *name; char *name; public: public: instructor(char *ip) {instructor(char *ip) { name = new char[50]; name = new char[50]; strcpy(name, ip); strcpy(name, ip);} } ~instructor(){delete[] name;} ~instructor(){delete[] name;} }; };01/14/19 10Dale RobertsClass Objects as Members -- ExampleClass Objects as Members -- Exampleclass csci_c{ class csci_c{ student s1, s2; student s1, s2; instructor i1; instructor i1; char *location; char *location; public: public: //Constructor Initializer //Constructor Initializer csci_c(char *n1, char *n2, char *n3, char *l) csci_c(char *n1, char *n2, char *n3, char *l) :s1(n1), s2(n2), i1(n3){:s1(n1), s2(n2), i1(n3){ location = new char[50]; strcpy(location, location = new char[50]; strcpy(location, l);l);} } ~csci_c(){delete[] location;} ~csci_c(){delete[] location;} }; }; main() {main() { csci_c csci_c fall_class("John","Tom","Henry","SI102");} fall_class("John","Tom","Henry","SI102");}01/14/19 11Dale RobertsConstructor InitializerConstructor Initializer Specifies the Processing to be Performed Specifies the Processing to be Performed Before the Actual Constructor Processing Starts Before the Actual Constructor Processing Starts A List of Entries, Each of Which has: A List of Entries, Each of Which has: Identifier of Fundamental Data Type Identifier of Fundamental Data Type Identifier of a Class Object Identifier of a Class Object Name of a Base Class -- Inheritance Name of a Base Class -- Inheritance Each Name is Followed by Its Initialization Each Name is Followed by Its Initialization Values in Parenthesis Values in Parenthesis Appropriate Constructors are Invoked Appropriate Constructors are Invoked01/14/19 12Dale RobertsConstructor Initializer -- ExampleConstructor Initializer --


View Full Document

IUPUI CS 265 - Classes

Download Classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Classes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?