190 likes | 538 Views
Department of Computer and Information Science, School of Science, IUPUI. Classes Access Specifiers. CSCI 240. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. Accessing Rights. Private: Member Functions of the Class and its Friends
E N D
Department of Computer and Information Science,School of Science, IUPUI ClassesAccess Specifiers CSCI 240 Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu
Accessing Rights • Private: Member Functions of the Class and its Friends • Protected: Member Functions and Derived Classes • Public: Entire World • Thumb Rule -- Data Members are Private and Member Functions are Public
Accessing Rights – Example 1 • // date.h/* A Simple Date Class With a Proper Accessing */ #define size 50 class date{ private: int day, month, year; char *string_date; public: date(char *ip_date); //Constructor ~date(); //Destructor void print_date(); //Member Function };
Accessing Rights – Example 1 (cont) • // date.cpp#include “date.h”date::date(char *ip_date){ day = 0; month = 0; year = 0; string_date = new char [size]; strcpy(string_date, ip_date);} date::~date() {delete[] (string_date);} void date::print_date() {cout << "Date is: " << string_date << endl;}
Accessing Rights – Example 1 (cont) • // client.cpp#include “date.h”main(){ date today("June 19, 1995"); // ERROR cout << "Date is: " << today.string_date << endl; today.print_date(); //CORRECT }
Accessing Rights - Example 2 • // student.h/* A Simple Class to Represent a Student */#define size 50class Student{ private: char *name; char *id; char *email; public: Student(char *, char *, char *); ~Student(); void printData();};
Accessing Rights – Example 2 …. • // student.cpp#include student.h/* Member functions of the “Student" class */Student ::Student(char *studentName, char *studentId, char *studentEmail){ name = new char[size]; strcpy(name, studentName); id = new char[size]; strcpy(id, studentId); email = new char[size]; strcpy(email, studentEmail);}Student::~Student(){ delete [] name; delete [] id; delete [] email;}void Student::printData(){ cout << “Name: ” << name << endl << “ID : ” << id << endl << “Email : ” << email << endl;}
Accessing Rights – Example 2 …. • // client.cpp#include “student.h”/* “csStudent1" is an instance of “Student" */main(){ Student csStudent1 ("Susie Creamchese“, "123456789“, "screamch@iupui.edu"); csStudent1.printData();}
Acknowledgements • These slides were originally prepared by Rajeev Raje, modified by Dale Roberts.