1 / 14

Constructing Objects

Constructing Objects. Classes – not for education only (or are they) OR Political correctness is not for objects. Typedef. Aliasing types (or classes) typedef int Age; typedef string Name; typedef float Distance_in_Meters; typedef float Money_in_Ks;// dollars. Classes.

cstephen
Download Presentation

Constructing Objects

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. Constructing Objects Classes– not for education only (or are they) OR Political correctness is not for objects

  2. Typedef • Aliasing types (or classes) typedef int Age; typedef string Name; typedef float Distance_in_Meters; typedef float Money_in_Ks;//dollars Gene Itkis: cs112, lec.3

  3. Classes • Classes are “data types” • Programmer-defined • Pre-defined: • int, float, bool, etc. – are all classes • Every object must belong to a class Gene Itkis: cs112, lec.3

  4. A class is born… class ClassName{ public: // constructors;member functions/fields; protected: data fields/functions;private: data fields/functions;}; ClassName VarName; //instantiation Gene Itkis: cs112, lec.3

  5. Protecting Privacy • Public • The Interface – accessible by non-members and members alike • Private • Accessible only by members • Protected • Same as private, except for friends • Real classes should not have public variables– they would have public methods (functions) Gene Itkis: cs112, lec.3

  6. Class Action Class Action { public: bool Sue(Name Target, Money_in_Ks Ripoff) {…}; //function def skipped protected: Money_in_Ks Bribe; }; Action MyLawyer; Gene Itkis: cs112, lec.3

  7. Addressing Class Methods • How to address members of the class objects: MyLawyer.Sue(“Mr.BigBucks”, 100000.0); • My Lawer’s “friends” (from “BigBucks”) could access MyLawyer.Bribe = 100000.1; Gene Itkis: cs112, lec.3

  8. Example: Pair class pairOfInts { public: int firstInt; int secondInt; }; pairOfInts GameScore; GameScore.firstInt=1; GameScore.secondInt=4; //typical Red Sox score Gene Itkis: cs112, lec.3

  9. Example: Instructor class instructor { public: string name; string course; int num_students=0; }; Gene Itkis: cs112, lec.3

  10. Example 1 revisited class instructor { public: string name; string course; int numStudents(student studentList[100]) { return countStudents( StudentList, course ); } // countStudents() defined elsewhere }; Gene Itkis: cs112, lec.3

  11. Example: Pair revisited(on board) class pairOfInts { public: int firstInt; int secondInt; }; pairOfInts GameScore; GameScore.firstInt=1; GameScore.secondInt=4; //typical Red Sox score Gene Itkis: cs112, lec.3

  12. Crypto example • Baby RSA class RSA { public: int encrypt(int clear_text) { return pow(clear_text, pub_key)%modulus; } private: int modulus, pub_key;} RSA GeneRSA; … Cipher_text = GeneRSA.encrypt(message); Gene Itkis: cs112, lec.3

  13. Pointing at Objects float MyMoney; float* pMyMoney=&MyMoney; //ptr to //addr of Student S; Student* pS=&S; (*pS).gpa = 3.5; // object pointed at pS->gpa = 3.5 // same as above Gene Itkis: cs112, lec.3

  14. More on classes – in later classes To be continued…

More Related