1 / 17

Introduction and Basics in

Learn the basics of object-oriented programming, including the concept of objects, classes, and encapsulation. Understand how to define data and functions in a class, and how to create objects and access their members. Explore the use of constructors and the different types available.

asean
Download Presentation

Introduction and Basics in

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. Introduction and Basics in Object Oriented programming

  2. Object Oriented Paradigm • Centers on the idea of all programs can be made up of separate entities (Objects) team up to exact units (Classes) .. WHY!!!! • Student (class): Sara, Amal, Nora, and Nada (objects) • Course (class): CS140, CS141, and CS 202 (objects) • Way of refereeing to the related collection of data within a unit (class) by a single variable name (object) • Sara (object): name (data 1), ID (data 2) and GPA (data 3) • Which is the unit here?

  3. Class • A user-defined data type that combine data and operations in a single unit • Encapsulation • Considered as an object factory which can be generate a number of similar objects • Share the same meaning, attributes (state) and operations (behavior) • Syntax: ClassclassNameIdentifier { //member functions //data members };

  4. Class Members • Can be data or functions • Each class member has a visibility • Indicated by access specifiers • Private: members of a class not accessible outside that class • Perform data hiding • By default, all members in a class are private • Public: members of a class accessible outside that class

  5. Class Members - Data • Known as attributes; declared like any other variables but can’t be initialized in the definition • Can be built-in data types or user-defined data types • Hold information for each object • Each object has its own copy of data members • Exist throughout the life of the object • Have the same name in all object but might have different values • Example: Class Student { // ---here member functions--- private: string name; int ID; double GPA; };

  6. Class Members - Functions • Known as methods; any function belong to class definition • Must be declared inside a class definition but can be defined outside that class definition • Outside class: by tide function name with class name via scope resolution operator (::) • Syntax: //Outside class returnType className::funName(parameters list) {body} • Follow the same regulations of functions in structured programming .. List them!!!

  7. Class Members - Functions • Types • Access functions: mutators, accessors and predicate functions • Mutators and accessors allow to interact with private data members either inside or out side class scope • Mutators may perform validation .. WHY here!!! • Utility functions • Local variable • Declared in a member function definition's body • Used only inside a member function • Its value lost when a member function terminates • When have the same name of data members • Precede data members with scope resolution operator (::)

  8. Class Members - Functions (Cont.) • Example: Class Student { public: //Accessors string getName(); int getID(); double getGPA(); //Mutators void setName(string name); void setID(int m=0); void setGPA(double g); //predicate bool isRegistered(); //Utility bool validateName(string n); private: string name; int ID; double GPA; };

  9. Class Members - Functions (Cont.) • Example: //File scope string Student::getName() { return name; } int Student::getID() { return ID; } double Student::getGPA() { return GPA; } void Student::setName(string name) { if(validateName(name)) ::name=name; else ::name= “”; } void Student::setID(double m=0) { ID=m; } void Student::setGPA(double g ) { GPA=g; } bool Student::isRegistered() { if( ID != 0 ) return true; elsereturn false; } bool Student::validateName(string n); { if( n.length() > 15 ) return false; elsereturn true; }

  10. Object • An object is a unique instance of a class Student Class Sara Amal name sara name amal ID ID 43011 43012 4.8 3.12 GPA GPA

  11. Object (Cont.) • Can’t use any class members until create an object of that class • To access an object’s public members by using • Dot operator (.) • Used with an object’s name or with a reference to an object • Example: //main function Student Sara; Sara.setID();

  12. Constructor • Special member function used to initialize an object’s data once created • It has: • The same name of a class • No return type; not even void • Can be overloaded • Syntax: public:className( parameter list) {constructor body}

  13. Default Constructor • Has no parameters • Two ways to provide a default constructor • By programmer; defines a default constructor explicitly • By compiler; generate a default constructor implicitly, if no constructors are provided for a class • Only initialize string data members to null string • Example: public: Student(){name=“N/A”; ID=0; GPA=0.0;}

  14. Parameterized Constructor • Allow to create a new object of a class while concurrently passing arguments to that object • Must supply the arguments to the constructor if no default arguments are provided • Example: public: Student(string n, int m, double g=0.0) {name=n; ID=m; GPA=g;}

  15. Do Use each access specifier only once in class definition Perform least privilege .. !!! Don’t Define a function inside another function Define a function parameter again as a local variable Initialize a data member of a class explicitly Do .. Don’t - Common errors

  16. Exercise -1 Write a class called Pen, which has two attributes: inkLevel, and color. A new pen always comes with inkLevel of 10, while the color is different from one pen to another. In addition to the constructor, a pen has a method called write which reduces the level of the ink the pen by 1, then returns the current inkLevel. A refill method sets the inkLevel to 10. Finally, a display method prints on the screen the details of the pen (its color and inkLevel).

  17. Exercise -2 Write a WebPage class which includes: • The following integer data members: • Number of hits on the page • Number of messages posted on the page • Number of downloads from the page • The following member functions • Default constructor initializes all data members to zero • Parameterized constructor initializes all data members to received values • An increment function for each data members to increase them by 1 • Activity() function that return total activity of webpage: hits + messages + downloads • Write a main function that declare and initialize imamUnivPage object. • Write the statements that increase number of hits by 2, messages by 3, and downloads by 1. • Display the number of hits, messages, and downloads for the page as well as the total activity.

More Related