IT college Second Semester 2018/2019 Object Oriented Programming Lab Lab #3: Classes and objects Objective: After performing this lab, the students should be able to: 1. Write the definition of the class 2. Write the implementation of the function members for the class outside and inside the class definition. 3. Work with class members. 4. Distinguish between the categories of class members (public and private) 5. Write the object Instantiation Class Definition: Classes are user-defined (programmer defined) types. Why we use classes? - To encapsulate the data (attributes) and functions (behavior) into packages called classes. A class definition begins with the keyword class. The body of the class is contained within a set of braces, { }; class person { . . . }; Note: person is the class name Access Modifiers Access modifiers are used to implement an important feature of Object Oriented Programming known as Data Hiding. (1) Access modifiers or Access Specifiers in a class are used to set the accessibility of the class members. There are 3 types of access modifiers: 1. Public: Can be accessed outside the class directly. 2. Private: Accessible only to member functions of class. 3. Protected Note: If we do not specify any access modifiers for the members inside the class then by default the access modifier for the members will be Private. class person { private: char PName[25]; int* Age; double points[4]; public : void setName(char N[25]); void setAge(int A); void setPoints(double p[4]); char * getName(); int getAge(); double* getPoints(); double getMaxPoint(); }; Implementation of the function members: There are 2 ways to define a member function: Inside class definition Outside class definition Method #1 (inside class): #include <iostream> using namespace std; //class definition class person{ private: char PName[25]; int* Age; double points[4]; public : void setName(char N[25]) { strcpy_s(PName, 25, N); } void setAge(int A) { *Age = A; } void setPoints(double p[4]) { for (int i = 0; i < 4; i++) points[i] = p[i]; } char * getName() { return PName; } int getAge() { return *Age; } double* getPoints(){ return points; } void checkAge(){ if (*Age >= 18) cout << "Legal Age!" << endl; else cout << "IlLegal Age" << endl; } double getMaxPoint(){ double Max = points[0]; for (int i = 1; i < 4; i++) { if (points[i] > Max) Max = points[i]; } return Max; } }; Method #2 (outside class): To define a member function outside the class definition we have to use the scope resolution :: operator along with class name and function name. Data_return_type class_name :: function_name () void person :: setName(char N[25]) Example: #include <iostream> using namespace std; //class definition class person{ private: char PName[25]; int* Age; double points[4]; public: void setName(char N[25]); void setAge(int A); void setPoints(double p[4]); char * getName(); int getAge(); double* getPoints(); double getMaxPoint(); void checkAge(){ }; void person:: setName(char N[25]) { strcpy_s(PName, 25, N); } void person::setAge(int A) { *Age = A; } void person:: setPoints(double p[4]) { for (int i = 0; i < 4; i++) points[i] = p[i]; } char * person::getName() { return PName; } int person::getAge() { return *Age; } double* person::getPoints(){ return points; } void person::checkAge(){ if (*Age >= 18) cout << "Legal Age!" << endl; else cout << "IlLegal Age" << endl; } double person::getMaxPoint(){ double Max = points[0]; for (int i = 1; i < 4; i++) { if (points[i] > Max) Max = points[i]; } return Max; } Set and Get functions: We use the set and get method to change and access the private member from outside the class. 1. Set method: The set method starts with void, because it does not return any values. void setName(char N[25]) { strcpy_s(PName, 25, N); } 2. Get method: The Get method starts with data return type (i.e, double, float, int ..etc.), because it returns the value char * getName() { return PName; } Note: char * equivalent to array of char Creating an object of a Class: When a class is defined, only the specification for the object is defined; no memory or storage is allocated. Once an object of a certain class is instantiated, a new memory location is created for it to store its data members and code Class_name object_name; Person per ; Note: You can instantiate many objects from a class type. Accessing Class Members: The data members and member functions of class can be accessed using the dot ( . ) operator with the object. Obj . class_member; Per . setName("x"); Note: Accessing a data member depends on the access modifier of that data member. - The public data members are accessed using the dot ( . ) operator - The private data members are not allowed to be accessed directly by the object. In the main: void main(){ person per; char name[25]; int A; double p[4]; cout << "ENTER PERSON NAME: "; cin.getline(name, 25); cout << "ENTER AGE: "; cin >> A; cout << "ENTER 4 POINTS FOR PERSON:\n"; for (int i = 0; i < 4; i++) cin >> p[i]; per.setName(name); per.setAge(A); per.setPoints(p); person per2; per2.setAll("Ahmad",30,p); cout << "PERSON NAME:\t" << per.getName() << endl; //cout << "person Age:\t" << per.getAge() << endl; cout << "PERSON POINTS:\t" << endl; double *ptr; ptr = per.getPoints(); // the array name points to the first element into array ptr-->points[0] for (int i = 0; i < 4; i++) cout << ptr[i] <<"\t"; cout << endl; cout << "CHECK THE PERSON AGE \t:"; per.checkAge(); cout << "THE MAXIMUN POINT IS \t:"<<per.getMaxPoint() << endl; } Full Example: #include <iostream> using namespace std; //class definition class person{ private: char PName[25]; int* Age; double points[4]; public : void setName(char N[25]) { strcpy_s(PName, 25, N); } void setAge(int A){ Age = new int; *Age = A; // OR Age = new int(A); } void setPoints(double p[4]) { for (int i = 0; i < 4; i++) points[i] = p[i]; } void setAll(char N[25], int A, double p[4]){ strcpy_s(PName, 25, N); Age = new int; *Age = A; for (int i = 0; i < 4; i++) points[i] = p[i]; } char * getName() { return PName; } int getAge() { return *Age; } double* getPoints(){ return points; } void checkAge(){ if (*Age >= 18) cout << "Legal Age!" << endl; else cout << "IlLegal Age" << endl; } double getMaxPoint(){ double Max = points[0]; for (int i = 1; i < 4; i++) { if (points[i] > Max) Max = points[i]; } return Max; } }; void main(){ person per; char name[25]; int A; double p[4]; cout << "ENTER PERSON NAME: "; cin.getline(name, 25); cout << "ENTER AGE: "; cin >> A; cout << "ENTER 4 POINTS FOR PERSON:\n"; for (int i = 0; i < 4; i++) cin >> p[i]; per.setName(name); per.setAge(A); per.setPoints(p); person per2; per2.setAll("Ahmad",30,p); cout << "PERSON NAME:\t" << per.getName() << endl; //cout << "person Age:\t" << per.getAge() << endl; cout << "PERSON POINTS:\t" << endl; double *ptr; ptr = per.getPoints(); // the array name points to the first element into array ptr-->points[0] for (int i = 0; i < 4; i++) cout << ptr[i] <<"\t"; cout << endl; cout << "CHECK THE PERSON AGE \t:"; per.checkAge(); cout << "THE MAXIMUN POINT IS \t:"<<per.getMaxPoint() << endl; } Lab work: 1- Add a new data member to the person class. 2- Add set and get functions for new members 3- Call them from the main Good Luck Islam Al-Dalabeeh