• No results found

13-Class and Object.pdf

N/A
N/A
Protected

Academic year: 2020

Share "13-Class and Object.pdf"

Copied!
22
0
0

Loading.... (view fulltext now)

Full text

(1)

OBJECT ORIENTED

PROGRAMMING

(2)

Content

2

Classes, Objects, Member Functions and Data

Members

Defining a Class with Members

Initializing Objects with Constructors

Class Scope and Accessing Class Members

(3)

Objectives

3

By the end you should recognize:

 What classes, objects, member functions and data members are

 How to define a class and use it to create an object

 How to define member functions in a class to implement the class's behaviors

 How to declare data members in a class to implement the class's attributes

 How to call a member function of an object to make that member function perform its

task

 The differences between data members of a class and local variables of a function

 How to use a constructor to ensure that an object's data is initialized when the object is

created

 Class scope and accessing class members via the name of an object

(4)

4

#include<iostream> #include<string> using namespace std;

int makeAppointmnet();

int deleteAppointment();

void addPatient();

void deletePatient;

void addStaff();

double salary();

int main() {

string staffName; int staffID;

string staffPhoneNumber; string patientName;

string patientID;

string patientPhoneNumber; string appointmentDate; return 0; }

• Suppose

we

were

writing

hospital software

• This a bad representation of a

hospital

• We need to separate bits of

information.

• Need a way of referring to

the whole collection with a

(5)

5

class appointment {

public:

int makeAppointmnet();

int deleteAppointment();

};

(6)

Patient Class

6

// C++ code to define a staff class

class staff{ private:

// Data Members

string staffName; int staffID;

string staffPhoneNumber; public:

// Member Fucntions

(7)

7 #include <iostream>

#include <string> using namespace std;

class staff{ private:

// Data Members

string staffName; int staffID;

string staffPhoneNumber; public:

// Member Fucntions

int addStaff(); double salary(); }; int main() { staff s1; s1.staffName="customer1"; s1.staffID =4034;

cout << s1.salary();

return 0; }

This program will produce 2 compilation errors:

#1: ‘staff:: staffName ' #2: ‘staff:: staffID ' :

(8)

Access Specifiers

Access Specifiers

public

: members of a class accessible outside that class

private:

members of a class not accessible outside that class

(9)

Using Access Specifiers

9

#include <iostream> #include <string> using namespace std; class staff{

public:

// Data Members

string staffName; int staffID;

string staffPhoneNumber;

// Member Fucntions

int addStaff(); double salary(); }; int main() { staff s1; s1.staffName="customer1"; s1.staffID =4034;

cout << s1.salary(); return 0;

}

(10)

Using Access Specifiers (cont.)

10

#include <iostream> #include <string> using namespace std; class staff{

private:

// Data Members

string staffName; int staffID;

string staffPhoneNumber; public:

// Member Fucntions

int addStaff(); double salary(); }; int main() { staff s1; s1.staffName="customer1"; s1.staffID =4034;

cout << s1.salary();

return 0; }

Anything following this can be used by anyone outside the class

(11)

Variables

Local variables

Variables declared in a function definition’s body

Cannot be used outside of that function body

When a function terminates

The values of its local variables are lost

Data Members

Variables in a class definition

Exist throughout the life of the object

Each object of class maintains its own copy of attributes

(12)

Set and Get Functions

public

member functions that allow users of an object to set or

get the values of private data members

Allows the creator of the class to control how clients access

private

data

(13)

Using Set and Get Functions

13

#include <iostream> #include <string> using namespace std;

class staff{ private:

// Data Members

string staffName; int staffID;

string staffPhoneNumber; public:

// Member Fucntions

int addStaff(); double salary(); };

How would you adjust the class definition to include set and get functions??

How many do you need ??

Should they be private/public ??

(14)

Implementing Set & Get Functions

For each data member

Set function that sets its value

Get function that returns its value

Staff Data Members

14

string staffName;

 void setStaffName(string);

(15)

Get Functions

15

string getStaffName() { return staffName; }

int getStaffID() {

return staffID; }

(16)

16 #include <iostream>

#include <string> using namespace std; class staff{

private:

// Data Members

string staffName; int staffID;

string staffPhoneNumber; public:

// Member Fucntions

int addStaff(); double salary();

string getStaffName() { return staffName; }

int getStaffID() { return staffID; }

string getStaffPhoneNumber() { return staffPhoneNumber; }

};

int main(){

staff s1;

cout<< s1.getStaffName(); return 0;

(17)

Set Functions -1

17

void setStaffName(string s) {

staffName=s; }

void setStaffID(int id) {

staffID=id; }

void setStaffPhoneNumber(string pn) {

(18)

18 #include <string>

using namespace std; class staff{

private:

// Data Members

string staffName; int staffID;

string staffPhoneNumber; public:

// Member Fucntions

int addStaff(); double salary();

string getStaffName() { return staffName; }

int getStaffID() { return staffID; }

string getStaffPhoneNumber() { return staffPhoneNumber; }

void setStaffName(string s) { staffName=s; }

void setStaffID(int id) { staffID=id; }

void setStaffPhoneNumber(string pn) { staffPhoneNumber = pn; }

};

int main(){

staff s1;

s1.setStaffName(“Maha Q”); cout << s1.getStaffName(); return 0;

(19)

Constructors

Functions used to initialize an object’s data when it is created

Call made implicitly when object is created

Must be defined with the same name as the class

Cannot return values

Not even void

(20)

Using Constructors -1

20 class staff{ private: string staffName; int staffID; string staffPhoneNumber; public:

string getStaffName() { return staffName; }

int getStaffID() { return staffID; }

string getStaffPhoneNumber() { return staffPhoneNumber; }

void setStaffName(string s) { staffName=s; }

void setStaffID(int id) { staffID=id; }

void setStaffPhoneNumber(string pn) { staffPhoneNumber = pn; } staff() {

staffName = “raha”; staffID = 123;

staffPhoneNumber = “4343434”

(21)

Exercise - 1

21

- Write a WebPage class which includes

The following integer data members

- Number of hits on the page

- Number of messages posted on the page

- Number of downloads from the page

The following member functions

- Default constructor initializes all data members to zero

- An increment function for each data members to increase them by 1

- Activity() function that return total activity of webpage: hits + messages + downloads

- Write a main function that declare and initialize imamUnivPage object.

- Write the statements that increase number of hits by 2, messages by 3, and

downloads by 1.

(22)

Included Sections

22

References

Related documents

Zell: Artificial Intelligence (after Russel/Norvig, 3rd Ed.) 58 (b) The recursion unwinds and the best leaf value of the forgotten subtree (417) is backed up to Rimnicu Vilcea;

Commercial Financial Technical Execution Engineering &amp; Procurement Construction &amp; Startup Project Execution Planning Front-End Engineering Design Strategic Project

SANDVIK CNMA 12 04 08 H1P (uncoated carbide) inserts were used throughout. conventional emulsified mineral oil based flood cooling system was utilized. The three main

Bicolored or uniformly dark, with microsculpture mesh pattern slightly transverse, sculpticells slightly wider than long (Fig. 5C-D) in dorsal aspect slender, shaft subsinuate,

matsumurai Sasaji in elytra with two spots (Figs 21, 24), but can be distinguished from the latter by its smaller size and unique male genitalia: penis short, apical 1/2 length

Instead, we describe a practi- cal method for incorporating the value of learning in sponsored search auctions, and analyze its revenue and efficiency impacts through simulations

Conclusions : The fluorescence technique involving vitamin B2 and a photodynamic diagnosis endoscope system enabled accurate and safe identification of the

 Presence of cancer care infrastructure such as medical laboratory, ultrasound facilities, X-ray facilities, Radio- and chemo- therapy facilities, nuclear