c++ classes programming in c++ fall 2008

10
C++ Classes Programming in C++ Fall 2008 Dr. David A. Gaitros [email protected] .edu

Upload: ornice

Post on 25-Feb-2016

28 views

Category:

Documents


1 download

DESCRIPTION

C++ Classes Programming in C++ Fall 2008 . Dr. David A. Gaitros [email protected]. C++ Classes. class Time { public: Time(); void setTime(int, int, int) void print24Hour(); void print12Hour(); private: int hour; int minute; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: C++ Classes Programming in C++ Fall 2008

C++ ClassesProgramming in C++

Fall 2008

Dr. David A. [email protected]

Page 2: C++ Classes Programming in C++ Fall 2008

C++ Classes

class Time { public: Time(); void setTime(int, int, int) void print24Hour();

void print12Hour(); private: int hour; int minute; int second; };

Page 3: C++ Classes Programming in C++ Fall 2008

C++ Classes

• A “class” is very similar to a “struct” but in many ways extends the capabilities and features. – A “struct” is just the data – A “struct” cannot hide the

implementation• The programmer must have an

intimate knowledge of the structure in order to use it

• The programmer has complete access to the structure

Page 4: C++ Classes Programming in C++ Fall 2008

C++ Classes

• A “class” can have both data and member functions incorporated into a single package.

• A “class” can incorporate data and function hiding – The programmer of user need not

have access to all of the data– The programmer or user need not

have access to all member functions

Page 5: C++ Classes Programming in C++ Fall 2008

C++ Classes

• Here is how it “usually” works:

Mainprogram.cpp

someclass.h

someclass.cpp

The main program includesthe header file “someclass.h”. This file has prototypes for all of the public member functions and public dataitems, if any. There is no actual executable code in “someclass.h”

“someclass.cpp contains theimplementation of the class. It not only includes all of the information in “someclass.h” but also all of the hidden or private member functions and data. It must “#include” the someclass.h file.

Page 6: C++ Classes Programming in C++ Fall 2008

C++ Classes

// MainProgram.cpp#include <iostream.h>#include "someclass.h"

Time TodaysDate;int main(void){ TodaysDate.printStandard(); return 0;}

Page 7: C++ Classes Programming in C++ Fall 2008

C++ Classes//someclass.h#ifndef TIME_H#define TIME_H

class Time{public:

Time(); Time(int h, int m, int s); ~Time(); void setTime(int h, int m, int s); void printMilitary(); void printStandard();

private: int hour; int minute; int second;

}; #endif

Page 8: C++ Classes Programming in C++ Fall 2008

C++ Classes//Someclass.cpp#include <iostream>using namespace std;#include "someclass.h"

Time::Time(int h, int m, int s){

hour = h; minute =m; second = s;

}Time::Time(){

hour =12; minute = 15; second = 0;

}Time::~Time(){

cout << "Time has stopped";}

Page 9: C++ Classes Programming in C++ Fall 2008

C++ Classes// someclass.cpp continuedvoid Time::setTime(int h, int m, int s)

{ hour = h; minute =m; second = s;

}void Time::printMilitary()

{ cout

<<hour<<":"<<minute<<":"<<second;}

void Time::printStandard(){

int tempHour; if(hour>12) tempHour = hour-12; else tempHour = hour; cout

<<tempHour<<":"<<minute<<":"<<second;}

Page 10: C++ Classes Programming in C++ Fall 2008

C++ Classes

# Makefile – This is a comment# -g turns on debugging options# -I. allows the current directory to contain system # include files. # -c tells the compiler to compile only, don’t link. # -Wno-deprecated turns off deprecated warning messages. # -o names the output file instead of the default a.out#

CC = g++ND = -Wno-deprecatedCFLAGS = -g -I. -c $(ND)LFLAGS = -g $(ND)

MainProgram: MainProgram.o someclass.o$(CC) $(LFLAGS) -o MainProgram someclass.o MainProgram.o

MainProgram.o: MainProgram.cpp someclass.o$(CC) $(CFLAGS) MainProgram.cpp

someclass.o: someclass.h someclass.cpp$(CC) $(CFLAGS) someclass.cpp