OOP_Intro_1

OOP_Intro_1 - Classes OOP& Class Object Terminology File Management Constructor and Destructor Member Functions Why Object-Oriented

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Classes OOP & Class Object Terminology File Management Constructor and Destructor Member Functions Why Object-Oriented Programming (OOP) ? • Object-oriented programming (OOP) is a programming Object-oriented paradigm that uses "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. • Programming techniques may include features such as Programming • Information hiding Information • Data abstraction Data • Encapsulation Encapsulation • Modularity Modularity • Polymorphism Polymorphism • Inheritance Inheritance Objects • Object is the basic unit of object-oriented Object programming. • Objects are identified by their unique name. Objects • An object represents a particular instance of a An class. • There can be more than one instance of an object. There • Each instance of an object can hold its own Each relevant data. relevant • An Object is a collection of data members and associated member functions also known as methods. Class • Defines the abstract characteristics of a thing (object), Defines including the thing's characteristics (its attributes, fields or properties) and the thing's behaviors (the things it can do, or methods, operations or features). • For example, the class Dog would consist of traits For shared by all dogs, such as breed and fur color (characteristics), and the ability to bark and sit (behaviors). • Classes provide modularity and structure in an objectoriented computer program. oriented • Collectively, the properties and methods defined by a Collectively, class are called members. Object Oriented Programming With OOP and C++ ♠ ♠ well organized easy to read to design to modify reuse of code facilitated ♠ Analysis of Problem Design a class to represent an employee. Design Data needed: Operations: Name Gross salary, Net salary ross Net GetName() GetName() SetName(string) GetGross() SetGross(float) SetNet( float ) GetNet() GetNet() UML (Unified Modeling Language) A design tool for OOP UML Sample UML Format EMP Name Gross Gross Net Net GetName() SetName(string) GetGross() SetGross(float) EMP() EMP(string, float, float) SetNet(double) GetNet() GetNet() Name of the class Attributes: Data types Functions: Operation Functions: on data types on Class Declaration Syntax class Name { public: public members; // usually capitalized // usually functions private: private members; // usually variables }; Note Sample C++ Implementation class EMP { class private: private: float Gross , Net; float string Name; Name; public: public: string GetName() ; string void SetName(string) ; void float GetGross() ; float void SetGross(float) ; void EMP() ; EMP() EMP(string, float, double) ; EMP(string, void SetNet( ) ; void double GetNet() ; double }; UML Representation EMP Name Gross ross Net Net GetName() SetName(string) GetGross() SetGross(float) EMP() // Constructor EMP(string, float, double) SetNet(double) GetNet() GetNet() Example of a Class Declaration class EMP { class public: public: string GetName() ; string void SetName(string) ; void float GetGross() ; float void SetGross(float, float) ; void EMP() ; EMP() EMP(string, float, float) ; EMP(string, void SetNet( ) ; void double GetNet() ; double private: private: float Gross , Net; Gross string Name; Name; }; }; * A Few Terms     class EMP class member Net, GetNet class object (class instance) EMP E1 ; client Program that uses the class Objects An object is an instance of a class, the same way An object instance a variable is an instance of a simple data type. variable Similarity: int row; int row; This creates an instance of int called “row”. This instance EMP employee ; EMP employee This creates an instance of EMP called This instance “employee”. “employee”. Class Declarations class Student { public: U cout << Diago.ID; cout << John.ID; Private members are not accessible private: private: int ID; ˆ P Ñ ¦ }; }; Student Diago, Student John ; John class Student { public: Class Declarations int ID; ID; private: U cout << Diago.ID; cout << John.ID; }; }; Student Diago, Student John ; John What can you do with a class?  declare as many objects as you like  pass class objects as parameters in a function and return them as function values What you cannot do? What you can do? valid operators valid member selection hall.getGPA() ; hall assignment = hall = sand ; hall . Most built-in Most operators are invalid : invalid Arithmetic Arithmetic hall + rand Relational hall == rand hall I/O cin >> hall cin Specification File class Name { public: public members; // usually capitalized // usually functions private: private members; // usually variables }; Class Member Functions Syntax type function_name(argument list) { function body } Example void Set(int id, string name) { empno = id ; empname = name ; } * Default Constructors default constructor parameterless constructor, it initializes the variables to some default value, such as zero Example TimeType(); Student(); Some Terminology OOP object instance variable method message passing C++ class object or class instance private data member public member function function call (to a public member function) A Simple Program // program to read employee details and to // output the data // #include <iostream> #include <string> class employee { // Class Declaration private: private: string int empname; empname; empno; public: public: // Constructors are used to initialize // objects objects employee ( ) { // default constructor empno = 0 ; } employee ( int id, string name) { // Alternative constructor empno = id ; empname = name ; } void getvalue ( ) { // To read data into an object void cout << ”INPUT EMP NAME:”; cin >> empname; cout << ”INPUT EMP NO:”; cin >> empno; } void displayvalue ( ) { // To print the content of // // an object. cout<<”EMP NAME:”<<empname; cout<<EMP NO:”<<empno; } }; // end of class definition main() main() { employee e1; e1.getvalue(); e1.displayvalu(); } //Creation of Object Part of Some Client Code: Student hall; // declaration of an object of type Student // Student Student sand; // declaration of an object of type Student // hall.addGrade(84.3); cout << “Enter the grade: “; cin >> inGrade; sand.addGrade(inGrade); sand = hall; // member-by-member assigning Class Scope declaration Student double presley; Stu_Gpa; function call presley.getGPA(); Stu_Gpa = getGPA(); Stu_Gpa getGPA() set_Gpa(Stu_Gpa); SomeClass someObj ; someObj.getGPA(); Files specification.h client.cpp #include “specification.h” void main() implementation.cpp #include “specification.h” Specification File class Name { public: public members; // usually capitalized // usually functions private: private members; // usually variables }; Class Member Functions Syntax type Class_name::function_name(argument list) { function body } Example void TimeType::Set(int hour, int minu, int secon) { hrs = hour; mins = minu; secs = secon; } * Client File(s) #include “timetype.h“ #include <iostream> using namespace std ; int main() { TimeType appointment; appointment.Set(15, 30, 0); appointment.Write(); etc. return 0 ; } This is diary.cpp This diary.cpp file file Class Implementation A constructor is a special member function provided to constructor initialize private data members when a class object is created. ♠ do NOT have a return type (not even void) do void ♠ ♠ ♠ must have the same name as the class itself invoked when you declare an instance of a class invoked with a list of initial values with there should be a default constructor there default Example TimeType(int, int, int); Student(double, char, char, char, char); ...
View Full Document

This note was uploaded on 02/04/2011 for the course CSCI 101 taught by Professor Ghyam during the Spring '08 term at USC.

Ask a homework question - tutors are online