Download presentation
Presentation is loading. Please wait.
1
Classes Introduction CSCI 240
Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Classes Introduction Dale Roberts, Lecturer Computer Science, IUPUI
2
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
3
Structure of a Class Data Members Member Functions
Accessing Rights -- public, private and protected Data members are variables of either fundamental data types or user defined data types. Member functions provide the interface to the data members and other procedures and function. Thus, if any other object or function needs to access the data members, it should do so through the member functions of the class. The default member accessibility is private, meaning that only class members can access the data member or member function. A client uses the public interface meaning it accessed public members. In order to assure data stability of the class, it is recommended that only member functions be public, and the data members are accessed only via get and set functions and procedures.
4
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.
5
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); };
6
An Example of a Class // date.cpp #include “date.h” /* Member functions of the "date" class */ void date::set_date(int ip_day,int ip_month,int ip_year) {day = ip_day; month = ip_month; year = ip_year;} void date::set_string_date(char *ip_date) {string_date = new char[size]; strcpy(string_date, ip_date);}
7
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.
8
Class - Example 2 // student.h /* A Simple Class to Represent a Student */ #define size 50 class Student{ private: char *name; char *id; char * ; public: void setName(char *studentName); void setId(char *studentId); void set (char *student ); };
9
Class – Example 2 (cont) // student.cpp #include “student.h” /* Member functions of the “Student" class */ void Student::setName(char *studentName) {name = new char[size]; strcpy(name, studentName);} void Student::setId(char *studentId) {id = new char[size]; strcpy(id, studentId);} void Student::set (char *student ) { = new char[size]; strcpy( , student );}
10
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(" "); }
11
Acknowledgements These slides were originally prepared by Rajeev Raje, modified by Dale Roberts.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.