Oop lec 4(oop design, style, characteristics)

960 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
960
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
64
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Oop lec 4(oop design, style, characteristics)

  1. 1. OOP Design, Style, Characteristics LECTURE NO 4 COMSATS
  2. 2. Object Oriented Programming 2 Programmer thinks about and defines the attributes and behavior of objects. Often the objects are modeled after real-world entities. Very different approach than function-based programming (like C). OOP
  3. 3. Reasons for OOP 3 Abstraction Encapsulation Information hiding Inheritance Polymorphism Software Engineering Issues OOP
  4. 4. Object Oriented Programming 4 Object-oriented programming (OOP)  Encapsulates data (attributes) and functions (behavior) into packages called classes. So, Classes are user-defined (programmer-defined) types.   Data (data members) Functions (member functions or methods) In other words, they are structures + functions OOP
  5. 5. Class: Object Types 5 A C++ class is an object type. When you create the definition of a class you are defining the attributes and behavior of a new type. Attributes are data members. Behavior is defined by methods. OOP
  6. 6. Information Hiding 6 The interface to a class is the list of public data members and methods. The interface defines the behavior of the class to the outside world (to other classes and functions that may access variables of your class type). The implementation of your class doesn't matter outside the class – only the interface. OOP
  7. 7. Information Hiding (cont.) 7 You can change the implementation and nobody cares! (as long as the interface is the same). You can use other peoples classes without fear! OOP
  8. 8. Inheritence 8 It is possible to extend existing classes without seeing them. Add whatever new behavior you want. Example:   You have a class that represents a "student". Create a new class that is a "good student".  OOP Most of the behavior and attributes are the same, but a few are different – specialized.
  9. 9. Polymorphism 9 The ability of different objects to respond to the same message in different ways. Tell an int to print itself: Now tell a double: Now tell the Poly: OOP cout << i; cout << x; cout << poly;
  10. 10. Private vs. Public 10 Public data members and methods can be accessed outside the class directly. The public stuff is the interface. Private members and methods are for internal use only. OOP
  11. 11. C++ Program Structure • Typical C++ Programs consist of:– – A function main – One or more classes • OOP Each containing data members and member functions. 11
  12. 12. Classes in C++ 12 Tells the compiler what member functions and data members belong to the class.  A class definition begins with the keyword class followed by class name.  The body of the class is contained within a set of braces, { } ; (notice the semi-colon). • class class_name { …. …. …. }; OOP Any valid identifier Class body (data member + methods) methods
  13. 13. Classes in C++ 13 Within the body, the keywords private: and public: specify the access level of the members of the class.  the default is private. Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section. OOP
  14. 14. Classes in C++ 14 class class_name { private: … … … public: … … … }; OOP private members or methods Public members or methods
  15. 15. Classes in C++ 15 • Member access specifiers – public: • • Indicates that a member function or data member is accessible to other functions and member functions of other classes. can be accessed outside the class directly. – – private: • • • OOP The public stuff is the interface. Accessible only to member functions of class Private members and methods are for internal use only. May be applied to data members and member functions
  16. 16. Class Example 16 This class example shows how we can encapsulate (gather) a circle information into one package (unit or class) class Circle { private: double radius; public: void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; OOP No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. They are accessible from outside the class, and they can access the member (radius)
  17. 17. Accessing Data Members 17 Data members are available within each method (as if they were local variables). Public data members can be accessed by other functions using the member access operator "." (just like struct). OOP
  18. 18. Accessing class methods 18 Within other class methods, a method can be called just like a function. Outside the class, public methods can be called only when referencing an object of the class. OOP
  19. 19. Creating an object of a Class 19 Declaring a variable of a class type creates an object. You can have many variables of the same type (class).  Instantiation Once an object of a certain class is instantiated, a new memory location is created for it to store its data members and code You can instantiate many objects from a class type.  OOP Circle c; Circle *c;
  20. 20. C++ Gradebook Example Deitel & Deitel Fig 19.1 1 // Fig. 19.1: fig19_01.cpp 2 // Define class GradeBook with a member function displayMessage; 3 // Create a GradeBook object and call its displayMessage function. 4 #include <iostream> 5 using std::cout; 6 using std::endl; “using”:– add the name to the current scope 7 8 // GradeBook class definition 9 class GradeBook 10 { 11 public: 12 // function that displays a welcome message to the GradeBook user 13 void displayMessage() 14 { 15 16 cout << "Welcome to the Grade Book!" << endl; } // end function displayMessage 17 }; // end class GradeBook 18 19 // function main begins program execution 20 int main() 21 { 22 GradeBook myGradeBook; // create a GradeBook object named myGradeBook 23 myGradeBook.displayMessage(); // call object's displayMessage function 24 return 0; // indicate successful termination 25 } // end main OOP Welcome to the Grade Book! 20
  21. 21. C++ Gradebook Example 1 // Fig. 19.1: fig19_01.cpp 2 // Define class GradeBook with a member function displayMessage; 3 // Create a GradeBook object and call its displayMessage function. 4 #include <iostream> 5 using std::cout; 6 using std::endl; 7 8 // GradeBook class definition 9 Beginning of class definition for class GradeBook Beginning of class body class GradeBook 10 { 11 public: Access specifier public; makes members available to the public 12 // function that displays a welcome message to the GradeBook user 13 void displayMessage() 14 { 15 16 cout << "Welcome to the Grade Book!" << endl; Member function displayMessage returns nothing } // end function displayMessage 17 }; // end class GradeBook 18 End of class body 19 // function main begins program execution Use dot operator to call GradeBook’s member function 20 int main() 21 { 22 GradeBook myGradeBook; // create a GradeBook object named myGradeBook 23 myGradeBook.displayMessage(); // call object's displayMessage function 24 return 0; // indicate successful termination 25 } // end main OOP Welcome to the Grade Book! 21
  22. 22. C++ Gradebook Example 1 // Fig. 19.1: fig19_01.cpp 2 // Define class GradeBook with a member function displayMessage; 3 // Create a GradeBook object and call its displayMessage function. 4 #include <iostream> 5 using std::cout; 6 using std::endl; 7 8 // GradeBook class definition 9 class GradeBook 10 { 11 public: 12 // function that displays a welcome message to the GradeBook user 13 void displayMessage() 14 { 15 16 ss s cla in hi t of t of ma c obje riable an ring atic va a Decl autom I.e., allocated on The Stack as an cout << "Welcome to the Grade Book!" << endl; } // end function displayMessage 17 }; // end class GradeBook 18 19 // function main begins program execution 20 int main() 21 { 22 GradeBook myGradeBook; // create a GradeBook object named myGradeBook 23 myGradeBook.displayMessage(); // call object's displayMessage function 24 return 0; // indicate successful termination 25 } // end main OOP Welcome to the Grade Book! 22
  23. 23. Member Functions with Parameters 1 // Fig. 19.3: fig19_03.cpp 2 3 // Define class GradeBook with a member function that takes a parameter; // Create a GradeBook object and call its displayMessage function. 4 #include <iostream> 5 6 using std::cout; using std::cin; 7 using std::endl; 8 9 #include <string> // program uses C++ standard string class Include string class definition 10 using std::string; 11 using std::getline; 12 Member function parameter 13 // GradeBook class definition 14 class GradeBook 15 { 16 public: 17 // function that displays a welcome message to the GradeBook user 18 19 void displayMessage( string courseName ) { 20 21 22 cout << "Welcome to the grade book forn" << courseName << "!" << endl; } // end function displayMessage Use the function parameter as a variable 23 }; // end class GradeBook 24 25 // function main begins program execution 26 int main() 27 { 28 string nameOfCourse; // string of characters to store the course name 29 GradeBook myGradeBook; // create a GradeBook object named myGradeBook 30 OOP 23
  24. 24. Member Functions with Parameters (cont.) 24 31 // prompt for and input course name 32 cout << "Please enter the course name:" << endl; 33 getline( cin, nameOfCourse ); // read a course name with blanks 34 cout << endl; // output a blank line 35 36 // call myGradeBook's displayMessage function 37 // and pass nameOfCourse as an argument 38 myGradeBook.displayMessage( nameOfCourse ); 39 return 0; // indicate successful termination 40 } // end main Please enter the course name: CS101 Introduction to C++ Programming Welcome to the grade book for CS101 Introduction to C++ Programming! OOP Passing an argument to the member function
  25. 25. Useful Tidbits 25 A string  Represents a string of characters.  An object of C++ Standard Library class std::string   Defined in header file <string>. Not a character array as in C. Library function getline  Used to retrieve input until newline is encountered  Example  getline( cin, nameOfCourse );   OOP Inputs a line from standard input into string object nameOfCourse. Defined in header file <iostream>.
  26. 26. Data Members of a Class 26 Declared in the body of the class May be public or private Exist throughout the life of the object. Stored in class object. Each object has its own copy.  OOP May be objects of any type
  27. 27. Public and Private Members 1 2 // Fig. 19.5: fig19_05.cpp // Define class GradeBook that contains a courseName data member 3 4 5 // and member functions to set and get its value; // Create and manipulate a GradeBook object with these functions. #include <iostream> 6 7 8 9 10 11 12 13 using std::cout; using std::cin; using std::endl; 27 #include <string> // program uses C++ standard string class using std::string; using std::getline; 14 // GradeBook class definition 15 class GradeBook 16 { 17 public: 18 // function that sets the course name 19 void setCourseName( string name ) 20 { 21 courseName = name; // store the course name in the object 22 } // end function setCourseName 23 24 // function that gets the course name 25 string getCourseName() 26 { 27 return courseName; // return the object's courseName 28 } // end function getCourseName 29 OOP set function modifies private data get function accesses private data
  28. 28. Public and Private Members (continued) 30 // function that displays a welcome message 31 32 28 void displayMessage() { 33 // this statement calls getCourseName to get the 34 // name of the course this GradeBook represents 35 cout << "Welcome to the grade book forn" << getCourseName() << "!" 36 << endl; 37 } // end function displayMessage 38 private: 39 string courseName; // course name for this GradeBook 40 }; // end class GradeBook 41 42 // function main begins program execution 43 int main() Use set and get functions, even within the class private members accessible only to member functions of the class 44 { 45 string nameOfCourse; // string of characters to store the course name 46 GradeBook myGradeBook; // create a GradeBook object named myGradeBook 47 48 // display initial value of courseName 49 cout << "Initial course name is: " << myGradeBook.getCourseName() 50 << endl; 51 OOP Accessing private data outside class definition
  29. 29. Public and Private Members (continued) 29 52 // prompt for, input and set course name 53 cout << "nPlease enter the course name:" << endl; 54 getline( cin, nameOfCourse ); // read a course name with blanks 55 myGradeBook.setCourseName( nameOfCourse ); // set the course name 56 57 cout << endl; // outputs a blank line 58 myGradeBook.displayMessage(); // display message with new course name 59 return 0; // indicate successful termination 60 } // end main Initial course name is: Please enter the course name: CS101 Introduction to C++ Programming Welcome to the grade book for CS101 Introduction to C++ Programming! OOP Modifying private data outside class definition default setting from constructor is an empty string!!
  30. 30. Software Engineering Observation 30 As a rule of thumb, data members should be declared private Member functions should be declared public.  Except member functions that are accessed only by other member functions of the class. Often useful to have get and set functions  OOP To access private members in controlled ways
  31. 31. What happens here? 31 class foo { int i; // # elements in the array int a[10]; // array 10 at most // sum the elements which i is it ? int sum(void) { int i, x=0; for (i=0;i<i;i++) x+=a[i]; return(x); } ... OOP
  32. 32. Class Scope Operator :: 32 You can solve the previous problem using the "::" operator classname::membername. for (i=0;i<foo::i;i++) x+=a[i]; OOP
  33. 33. Method names and :: 33 Sometimes we put just a prototype for a member function within the class definition. The actual definition of the method is outside the class. You have to use :: to do this. OOP
  34. 34. Example class foo { ... int sum(void); }; 34 int foo::sum(void) { int sum=0; for (int i=0;i<foo::i;i++) { sum += a[i]; } return(sum); } OOP
  35. 35. Implementing class methods 35 Class implementation: writing the code of class methods.  There are two ways:  Member functions defined outside class 1.      Using Binary scope resolution operator (::) “Ties” member name to class name Uniquely identify functions of particular class Different classes can have member functions with same name Format for defining member functions ReturnType ClassName::MemberFunctionName( ){ … } OOP
  36. 36. Implementing class methods 36 2. Member functions defined inside class  OOP Do not need scope resolution operator, class name; class Circle { private: double radius; public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Defined inside class
  37. 37. class Circle { private: double radius; 37 public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Circle::Circle(int r) { radius = r; } double Circle::getArea() { return radius * radius * (22.0/7); } double Circle:: getCircumference() { return 2 * radius * (22.0/7); OOP } Defined outside class
  38. 38. Accessing Class Members 38 Operators to access class members   Identical to those for structs Dot member selection operator (.)    Arrow member selection operator (->)  OOP Object Reference to object Pointers
  39. 39. class Circle { private: double radius; 39 public: The first The second Circle() { radius = 0.0;} constructor is constructor is Circle(int r); called called void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); Since radius is a void main() double getCircumference(); private class data { }; Circle c1,c2(7); member Circle::Circle(int r) { cout<<“The area of c1:” <<c1.getArea()<<“n”; radius = r; } //c1.raduis = 5;//syntax error double Circle::getArea() c1.setRadius(5); { return radius * radius * (22.0/7); cout<<“The circumference of c1:” } << c1.getCircumference()<<“n”; double Circle:: getCircumference() { cout<<“The Diameter of c2:” <<c2.getDiameter()<<“n”; return 2 * radius * (22.0/7); } } OOP
  40. 40. class Circle { private: double radius; 40 public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; void main() Circle::Circle(int r) { { Circle c(7); radius = r; Circle *cp1 = &c; } Circle *cp2 = new Circle(7); double Circle::getArea() cout<<“The area of cp2:” { <<cp2->getArea(); return radius * radius * (22.0/7); } } double Circle:: getCircumference() { return 2 * radius * (22.0/7); } OOP

×