1 / 20

Classes

Classes. EECE 351 Spring 2000 Lecture # 11. Introduction. Overview – What is a class? The Details – How a class looks? The Reasoning – Why use a class? The Method – How do you use a class?. Overview What is a class?. Classes are what put the ++ in C. A class is: A user- defined type!

selia
Download Presentation

Classes

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. Classes EECE 351 Spring 2000 Lecture # 11 EECE 351 – Lecture 11

  2. Introduction • Overview – What is a class? • The Details – How a class looks? • The Reasoning – Why use a class? • The Method – How do you use a class? EECE 351 – Lecture 11

  3. OverviewWhat is a class? • Classes are what put the ++ in C. • A class is: • A user-defined type! • A way to group data with manipulating functions and/or operators (an object). • A type that defines the interface of a particular kind of object. • Also contains the implementation. EECE 351 – Lecture 11

  4. OverviewWhat is a class? (cont.) • However, classes are not objects. • Objects are instantiated classes. • Why do SEs like classes? • Classes allow different access levels to internal data. • This means we can hide some of the information from the user. • Data Abstraction – GOOD THING!!! • Classes open up a whole new world. • OOP EECE 351 – Lecture 11

  5. OOP Methodology Attributes • Data Abstraction • Inheritance • Polymorphism • Communication Through Messages Object Oriented Programming Data Abstraction Polymorphism Inheritance EECE 351 – Lecture 11

  6. Classes (cont.) OOP vs. “Old School” • OOP? • Object Oriented Programming • Code and data that belong together can be combined into objects. • Break the overall task over multiple objects with different data variables and member functions. • “Old school?” • Functional • Code and data are separate. • Divide task into steps each function accomplishes. EECE 351 – Lecture 11

  7. OverviewWhat is an object? • Think of an object like this: Chest of Drawers • Contains things you may or may not want others to see. • Sweaters, “tighty-whities” • Properties (Variables) • You can get things in & out of the chest by using a drawer (Method – function). • Good SEs always use the drawers!! EECE 351 – Lecture 11

  8. OverviewWhat is an object? (cont.) • Understanding the drawer concept • 1st, Put things in the drawer (Input) • Then, close the drawer and wait (Function CALL) • Finally, wait for drawer to pop open (Answer – what function returns, Output) • Use what you get back any way you want • This drawer concept is DATA ABSTRACTION EECE 351 – Lecture 11

  9. The DetailsHow a class looks? • A class has two parts. • Interface • Implementation • The (skeleton) interface looks similar to this: class CTeach //ClassName our “parents” go here too { //Begin interface list public: //Hold on, we’ll get to this in 1 min. CTeach(); //Constructor – 1st thing our class does ~CTeach(); //Destructor – Last thing our class does //Anything else we want };//End interface list ‘;’ is NEEDED HERE! EECE 351 – Lecture 11

  10. The DetailsHow a class looks? (cont.) • The (skeleton) implementation looks similar to this: CTeach::CTeach() { //Initialization goes here! Initialization is important } CTeach::~CTeach() { //Kill me code goes here. Usually used for deletes } <return type> CTeach::FuncName(<input type> pName, …) { //How the others look } EECE 351 – Lecture 11

  11. Constructors • NEVER have return types (not even void) • Can accept parameters • Begins the life of class members • Called automatically when class is instantiated CTeach::CTeach() //CTeach:: links func with class { //Initialize all members. SetSSN(262890329); //SetX is other class func SetName(“Adrian Michael Nida”); SetDOB(07111976); } EECE 351 – Lecture 11

  12. Destructors • NEVER have return types (not even void) • NEVER accept parameters • Ends the life of class members • Called by user orautomatically when scope ends • Usually used to give memory back to OS CTeach::~CTeach() //CTeach:: links func with class { } EECE 351 – Lecture 11

  13. The ReasoningWhy use a class? • The class language construct provides a way to link functions with data. • More importantly, the class construct enables specific types of access to the data. • public • Everyone can get to it • protected • Only this and derived classes can access • THERE IS NO NEED TO USE THIS ONE! • private • Only this class has the key EECE 351 – Lecture 11

  14. The ReasoningWhy use a class? (cont.) • Access levels let us put things in the chest with us. • Keeps others from getting our stuff • Locked Drawers (private Functions) • Interface goes in the .h file. Implementation goes in the .cpp file. • Member variables should always be private (and have the m_ prefix)! • Create “get” and “set” functions to encapsulate the variables. EECE 351 – Lecture 11

  15. The ReasoningWhy use a class? – Gets & Sets • If a class only has private data, how does the user access it? • Implementer “allows” access through certain public functions. • These functions all called: • <type> GetNAME() – Returns current value of NAME; • intSetNAME(<type> pNewVal) – Changes NAME to value of pNewVal. int is used for error checking. • Encapsulation!!! EECE 351 – Lecture 11

  16. The ReasoningWhy use a class? – Gets & Sets (cont.) • Why this extra step? • IT WILL SAVE YOU FROM ETERNAL CODING DAMNNATION! • Imagine when something goes wrong… • Why change Millions of NAME = XX; ? • When you can just change 1 SetName? • What could go wrong? • Porting – NT to LINUX (98 to 2000) • Location – feet vs. meters (NASA) • User errors – Grade of -5682 EECE 351 – Lecture 11

  17. The Method How do you use a class? • To use the member functions (or variables depending on access level) one must invoke the dot “.” operator. CHuman cMan; //Instantiate an object of type CHuman // Calls the CHuman function “Set” with the 3 parameters. cMan.Set(123456789, “Joe Doe”, 050178); // Attempts illegal access of private data. WILL NOT COMPILE!!! cMan.m_nDOB = 050178; EECE 351 – Lecture 11

  18. The Method How do you use a class? (cont.) • However, when a pointer to a class is used, one must invoke the structure pointer deference “->” operator. CHuman * pcMan = new CHuman; //Pointer to new CHuman // Call the “Set” function of what pcMan points to. pcMan->Set(123456789, “Joe Doe”, 050178); // Attempts illegal access of private data of what pcMan points to. WILL NOT COMPILE!!! pcMan->m_nDOB = 050178; delete pcMan; EECE 351 – Lecture 11

  19. What we know • Classes • How? • What? • Why? EECE 351 – Lecture 11

  20. Where we are going • LAB!!! • How to build a class using MSDEV • Lecture • More classes • so much to know… so little time • Homework • Critical Numbers • Remember the quadratic equation? EECE 351 – Lecture 11

More Related