Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes Access Specifiers

Similar presentations


Presentation on theme: "Classes Access Specifiers"— Presentation transcript:

1 Classes Access Specifiers
Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Classes Access Specifiers Dale Roberts, Lecturer Computer Science, IUPUI

2 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

3 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 };

4 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;}

5 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 }

6 Accessing Rights - Example 2
// student.h /* A Simple Class to Represent a Student */ #define size 50 class Student{   private: char *name; char *id; char * ;   public: Student(char *, char *, char *); ~Student(); void printData(); };

7 Accessing Rights – Example 2 ….
// student.cpp #include student.h /* Member functions of the “Student" class */ Student ::Student(char *studentName, char *studentId, char *student ){ name = new char[size];    strcpy(name, studentName); id = new char[size];    strcpy(id, studentId); = new char[size];     strcpy( , student ); } Student::~Student(){ delete [] name; delete [] id; delete [] ; } void Student::printData(){ cout << “Name: ” << name << endl << “ID : ” << id << endl << “ ” << << endl;}

8 Accessing Rights – Example 2 ….
// client.cpp #include “student.h” /* “csStudent1" is an instance of “Student" */ main(){     Student csStudent1 ("Susie Creamchese“, " “, csStudent1.printData(); }

9 Acknowledgements These slides were originally prepared by Rajeev Raje, modified by Dale Roberts.


Download ppt "Classes Access Specifiers"

Similar presentations


Ads by Google