210 likes | 327 Views
Object-Oriented Programming Using C++. CLASS 3. Objectives. To understand the software engineering concepts of encapsulation and data hiding To understand the notions of data abstraction and abstract data types (ADTs) To be able to create, use, and destroy class objects. Objectives.
E N D
Objectives • To understand the software engineering concepts of encapsulation and data hiding • To understand the notions of data abstraction and abstract data types (ADTs) • To be able to create, use, and destroy class objects
Objectives • To be able to control access to object data members and member functions • To begin to appreciate the value of object orientation
Declaring a Class class ledger // declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method };
Declaring a Class class ledger// declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method }; • Keyword class to specify that we’re declaring a class called ledger.
Declaring a Class class ledger// declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method }; • The body of declaration is delimited with curly brackets and terminated with a semicolon.
Declaring a Class class ledger// declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method }; • Within the class the variables list and count are declared
Declaring a Class • In a class, we can declare functions as well as variables. Functions in class are called methods. Declare the methods initCount( ), enterItem( ), and printTotal( ). class ledger// declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method };
Declaring a Class class ledger // declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method }; • Some members of class ledger are private, and some are public.
Declaring a Class class ledger // declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method }; • The data items are all private
Declaring a Class class ledger // declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method }; • The methods are all public
Declaring a Class • Some members of class ledger are private, and some are public. The data items are all private and the methods are all public. Public can be accessed from outside the object and private can be accessed only from within the object. The keyword private is the default. class ledger // declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method };
Declaring a Class class ledger // declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method }; • Declaration of ledger shown above does not define any objects of class ledger. It only specifies what objects will contain if they are defined.
void ledger::intCount(void) //a function in class ledger //initializes count to 0 { count = 0; } void ledger::enterItem(void) //a function in ... //puts entry in list { cout << “\nEnter amount:”; cin >> list[count++]; } void ledger::printTotal(void) // a function in... //prints total of all { float total = 0.0; for(int j=0; j<count; j++) total + = list[ j ]; cout << “\n\nTotal is: “ << total << “\n”; }
void ledger::intCount(void) //a function in class ledger //initializes count to 0 {count = 0; } void ledger::enterItem(void) //a function in ... //puts entry in list { cout << “\nEnter amount:”; cin >> list[count++]; } void ledger::printTotal(void) // a function in... //prints total of all { float total = 0.0; for(int j=0; j<count; j++) total + = list[ j ]; cout << “\n\nTotal is: “ << total << “\n”; } • initCount( ) simply sets the variable count to 0
void ledger::intCount(void) //a function in class ledger //initializes count to 0 { count = 0; } void ledger::enterItem(void) //a function in ... //puts entry in list { cout << “\nEnter amount:”; cin >> list[count++]; } void ledger::printTotal(void) // a function in... //prints total of all { float total = 0.0; for(int j=0; j<count; j++) total + = list[ j ]; cout << “\n\nTotal is: “ << total << “\n”; } • enterItem( ), prints a prompt to the user, and then assigns the amount typed by the user to the array member list[count].
printTotal( ) finds the total of the amounts and prints out the total void ledger::intCount(void) //a function in class ledger //initializes count to 0 { count = 0; } void ledger::enterItem(void) //a function in ... //puts entry in list { cout << “\nEnter amount:”; cin >> list[count++]; } void ledger::printTotal(void) // a function in... //prints total of all { float total = 0.0; for(int j=0; j<count; j++) total + = list[ j ]; cout << “\n\nTotal is: “ << total << “\n”; }