Docsity

Understanding Classes: Data Members, Member Functions, and Accessing Rights in OOP, Slides of Computer Science

Partial preview of the text

Download Understanding Classes: Data Members, Member Functions, and Accessing Rights in OOP and more Slides Computer Science in PDF only on Docsity!

Classes

Introduction

Class

  • An IMPORTANT FACET OF OOD and C++
  • A Set of Homogeneous Objects
  • An Abstract Data Type (ADT)
  • A Collection of Data Items or Functions or Both
  • An User Defined Type – describes a new data

type

  • Class Definition is a Type Declaration -- No Space

is Allocated

Syntax of a Class

class Class_Name{ private: //Default and Optional data_member_specification_1; : data_member_specification_n; public: data_member_specification_n+1; : data_member_specification_n+m;

}; //Don't forget the semicolon after the closing brace.

  • Each class declaration starts with the keyword class followed by the name of the class.
  • The definition of the class falls between the open and closed braces that follow the class name.

An Example of a Class

  • // date.h

/* A Simple Class to Represent a Date */ #define size 50 class date{ private: int day, month, year; char *string_date; public: void set_date( int ip_day,int ip_month,int ip_year); void set_string_date(char *ip_date); };

An Example of a Class

  • // client.cpp

#include “date.h” /* "today" and "yesterday" are instances of "date" */ main(){ date today, yesterday; today.set_date(19, 06, 1995); today.set_string_date("June 19, 1995"); yesterday.set_date(18, 06, 1995); yesterday.set_string_date("June 18, 1995"); }

  • Compile with

% g++ client.cpp date.cpp Later, we’ll learn to write a make file.

Class - Example 2

  • // student.h /* A Simple Class to Represent a Student */ #define size 50

class Student{ private: char *name; char *id; char *email; public: void setName(char *studentName); void setId(char *studentId); void setEmail(char *studentEmail); };

Class – Example 2 contd….

  • // client.cpp

#include “student.h”

/* “csStudent" is an instance of “Student" */

main(){

Student csStudent; // Allocates space

csStudent.setName("Susie Creamchese");

csStudent.setId("123456789");

csStudent.setEmail("[email protected]");