CSNB244 Lab 7 Classes in C Part I

Programming II with C++ (CSNB244) – Lab 7 Topics: Classes in C++ Part I Classes are an expanded concept of data structur...

2 downloads 107 Views 231KB Size
Programming II with C++ (CSNB244) – Lab 7 Topics: Classes in C++ Part I Classes are an expanded concept of data structures: like data structures, they can contain data members, but they can also contain functions as members. An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. Classes are defined using either keyword class or keyword struct, with the following syntax: class class_name { access_specifier_1: member1; access_specifier_2: member2; ... } object_names;

   

class_name is a valid identifier for the class object_names is an optional list of names for objects of this class members can either be data or function declarations access specifiers (optional) o private members of a class are accessible only from within other members of the same class (or from their "friends") o protected members are accessible from other members of the same class (or from their "friends"), but also from members of their derived classes o public members are accessible from anywhere where the object is visible

Example 1 class Rectangle { int width, height; public: void set_valuesF(int,int); //this is a function declaration int area (void); //this is a function declaration } rect;

Based on example 1; 1. Class name: ________________________ 2. Object for this class: ________________________ 3. Number of class members: ________________________ 4. List of the class members: _______________________________________________ 5. Class members with private access: ________________________ 6. Class members with public access: ________________________ Accessing the members Any of the public members of object rect can be accessed as if they were normal functions or normal variables, by simply inserting a dot (.) between object name and member name. This follows the same syntax as accessing the members of plain data structures. rect.set_valuesF (3,4); myarea = rect.area();

The only members of rect that cannot be accessed from outside the class are width and height, since they have private access and they can only be referred to from within other members of that same class. Page 1 of 4 COIT – NSMS

Nov 2013

Programming II with C++ (CSNB244) – Lab 7 Scope Operator :: Scope operator is used in the definition of function set_valuesF to define a member of a class outside the class itself. void Rectangle::set_valuesF(int x, int y) { width = x; height = y; }

Notice that the definition of the member function area has been included directly within the definition of class Rectangle given its extreme simplicity. Conversely, set_valuesF it is merely declared with its prototype within the class, but its definition is outside it. In this outside definition, the operator of scope (::) is used to specify that the function being defined is a member of the class Rectangle and not a regular nonmember function. For example, the function set_valuesF in the previous example has access to the variables width and height, which are private members of class Rectangle, and thus only accessible from other members of the class, such as this. //Example 1 #include using namespace std; class Rectangle { int width, height; public: void set_valuesF(int, int); int area(void) { return width*height; } }; void Rectangle::set_valuesF(int x, int y) { width = x; height = y; } int main() { Rectangle rect; rect.set_valuesF(3, 4); cout << "area: " << rect.area() << endl; system("pause"); return 0; }

Exercise 1 Based on Example 1, create one more object with name rectB. This object is use to call set_valuesF function and pass 2 values; width = 10, height = 20. Then print the value calculated by area function. Access to private data, private. By default, all members of a class declared with the class keyword have private access for all its members. Therefore, any member that is declared before any other access specifier has private access automatically.

COIT – NSMS

Page 2 of 4 Nov 2013

Programming II with C++ (CSNB244) – Lab 7 For example: class Rectangle { int width, height; };

private members of a class are accessible only from within other members of the same class (or from their "friends"). If you want to access width and height, you can include two functions to return the value of width and height. class Rectangle { int width, height; public: int accessWidth(void) { return width; } int accessHeight(void) { return height; } };

See the full codes; //Example 2 #include using namespace std; class Rectangle { int width, height; public: void set_values(int, int); //this is a function int area() { return width*height; } int accessWidth(void) { return width; } int accessHeight(void) { return height; } }; void Rectangle::set_values(int x, int y) { width = x; height = y; } int main() { Rectangle rect; rect.set_values(3, 4); cout << "height = " << rect.accessHeight() << endl << "width = " << rect.accessWidth() << endl; cout << "area: " << rect.area() << endl; system("pause"); return 0; }

COIT – NSMS

Page 3 of 4 Nov 2013

Programming II with C++ (CSNB244) – Lab 7 Based on example 2; 1. Class name: ________________________ 2. Object for this class: ________________________ 3. Number of class members: ________________________ 4. List of the class members: _______________________________________________ 5. Class members with private access: ________________________ 6. Class members with public access: ________________________ Exercise 2 The following program consist some errors. By maintaining the use of class, try to modify the program so that it can be executed correctly. #include using namespace std; class Fruit { float price, weight; public: void assignValue(float, float); //this is a function float calcPrice(void) { return price*weight; } }; void Fruit ::assignValue(float a, float b){ price = a; weight = b; } int main() { Fruit banana; banana.assignValue(20.00, 5); cout << "Price for Banana is RM" << banana.price << " per kg. I took " << banana.weight << "kg. Now I need to pay RM" << banana.calcPrice() << endl; }

Sample Output:

Exercise 3 Write a program to calculate these 4 arithmetic operations; sum, sub, multiply, and division. You should create a class. This class should have 2 private members to hold the data, 4 functions to calculate 4 arithmetic operations. Display the result of these calculations.

COIT – NSMS

Page 4 of 4 Nov 2013