Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

An introduction to classes, a fundamental concept in object-oriented programming (oop) and c++. Learn about the structure of a class, including data members, member functions, and accessing rights. Discover how to define and use classes through examples, such as the 'date' and 'student' classes.

Typology: Slides

2012/2013

Uploaded on 03/20/2013

dharmanand
dharmanand 🇮🇳

3.3

(3)

61 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Classes
Introduction
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

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("screamch@iupui.edu");