Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.

Similar presentations


Presentation on theme: "Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick."— Presentation transcript:

1 Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick

2 Classes – declaration & definition
Just like C++ global functions and global variables, C++ classes must be declared before they are used C++ classes must (generally) be implemented in two parts Declaration – in a header file Definition – in a .cpp file CS-1030 Dr. Mark L. Hornick

3 A C++ Class Declaration (in .h)
class keyword Name of the class class Employee { private: string name; int id; public: Employee(string nameArg, int idArg ); string getName(); int getID(); }; Attributes cannot be initialized Don’t forget the semi-colons! CS-1030 Dr. Mark L. Hornick

4 Class Definition Notes
public Visible to everyone private Visible only within the class Used to protect class objects protected Visible to classes which derive from the base class In Java, protected attributes and methods are also visible to other classes in the same package as well CS-1030 Dr. Mark L. Hornick

5 Defining a Class Member Function (in .cpp)
function’s full name return type function’s scope int Employee::getID () { return id; } Employee::Employee( string nameArg, int idArg ) { name = nameArg; id = idArg; } CS-1030 Dr. Mark L. Hornick

6 Class Constructors Job is to initialize the object
Called automatically, just like in Java Here is where attributes are initialized Same name as the class Just like Java No return type (not even void) Java constructors have the void type Can be overloaded CS-1030 Dr. Mark L. Hornick

7 Parameter names optional in declaration!
Constructor Example Declaration in .h file: class Employee { // note no “public” public: Employee(string nameArg, int idArg); …}; Definition in .cpp file: Employee::Employee(string name, int id) { // initialization code here } Parameter names optional in declaration! No return value CS-1030 Dr. Mark L. Hornick

8 Creating an instance of a C++ class
#include “employee.h” int main( int argc, char* argv[] ){ // note difference vs. Java: Employee pat( “Pat”, 123 ); Employee tim( “tim”, 456 ); int id1 = pat.getID(); // objects are destroyed here… } CS-1030 Dr. Mark L. Hornick

9 C++ Class Destructor No such method in Java
Cleans up an object at the end of lifetime Cleans up the data members prior to their own destruction Closes things (like files), etc. Called automatically Special name – similar to constructor Clock::~Clock() // note the ~ If you don’t supply a destructor, the compiler will! Compiler-supplied destructor does nothing! CS-1030 Dr. Mark L. Hornick

10 No arguments or return value
Destructor Example In .h file class Employee {… public: ~Employee(); …}; In .cpp file Employee::~Employee() {} // Do nothing No arguments or return value CS-1030 Dr. Mark L. Hornick


Download ppt "Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick."

Similar presentations


Ads by Google