1 / 9

Classes Access Specifiers

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

skeete
Download Presentation

Classes Access Specifiers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  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 50class Student{  private: char *name; char *id; char *email;  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 *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;}

  8. 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();}

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

More Related