1 / 10

C++ Classes Programming in C++ Fall 2008

C++ Classes Programming in C++ Fall 2008 . Dr. David A. Gaitros dgaitros@admin.fsu.edu. C++ Classes. class Time { public: Time(); void setTime(int, int, int) void print24Hour(); void print12Hour(); private: int hour; int minute;

ornice
Télécharger la présentation

C++ Classes Programming in C++ Fall 2008

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ ClassesProgramming in C++Fall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu

  2. C++ Classes class Time { public: Time(); void setTime(int, int, int) void print24Hour(); void print12Hour(); private: int hour; int minute; int second; };

  3. 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

  4. 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

  5. C++ Classes • Here is how it “usually” works: The main program includes the header file “someclass.h”. This file has prototypes for all of the public member functions and public data items, if any. There is no actual executable code in “someclass.h” Mainprogram.cpp someclass.h “someclass.cpp contains the implementation 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. someclass.cpp

  6. C++ Classes // MainProgram.cpp #include <iostream.h> #include "someclass.h" Time TodaysDate; int main(void) { TodaysDate.printStandard(); return 0; }

  7. 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

  8. 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"; }

  9. C++ Classes // someclass.cpp continued void 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; }

  10. 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-deprecated CFLAGS = -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

More Related