For illustrative purposes, we'll focus on a class named Simple
with a single int data member named val
, a default constructor, and
a single member function name getVal
.
// Simple.h class Simple { public: Simple(int val) : val(val) {} // outsideval
is data member; insideval
is parameter int getVal() {return val;} // inline function private: int val; // data members are part of class declaration };
get
/set
functions)
val
) refers to those of the receiving object
Simple s1(5), s2(10); // initilalizes s1's val to 5, and s2's to 10 cout << s1.getVal() << endl; // theval
insoidegetVal
will refer to that ofs1
cout << s1.getVal() << endl; // theval
insoidegetVal
will refer to that ofs2
this
in that case
this
by having a fixed format of the form data-member(expression)
.h
file
// Simple.h (interface/header) file class Simple { public: Simple(int val); // headers only int getVal(); private: int val; // data members are part of class declaration };
//Simple.cpp (implementation/source) file Simple::Simple() : val(val) {} int Simple::getVal() {return val;}
.h
file, and the implementation, i.e., the member function definitions (function bodies) go in the .cpp
file.
Simple::
uses the scope resolution operator to:
.h
file), while the others are placed
in the corresponding .cpp
file
.cpp
file is necessary
class Simple { public: Simple(int val); // headers only int getVal(); private: int val; // data members are part of class declaration }; inline Simple::Simple() : val(val) {} inline int Simple::getVal() {return val;}
.h
file AFTER the class declaration
inline
keyword
class Simple { public: Simple(int val); // headers only int getVal(); private: int val; // data members are part of class declaration }; inline Simple::Simple() : val(val) {} inline int Simple::getVal() {return val;}
.h
) file
.cpp
) file,
inline
keyword)
static
(Class) Membersstatic
data member and member functions have the same semantics as in Java — there is a single copy of
a static data member for the class, and static member functions take no receiver (instead they are invoked with the class name and
scope resolution operator, e.g. SomeClass::f()
) and can only refer to the static data member of the class.
static
keyword is used only in the class declaration, the subsequent definition does not contain
static
in the header.
.cpp
file
// Simple.h class Simple { … static int nextId; … };
// Simple.cpp int Simple::nextId = 1000;
Simple
that is based on the Simple
class illustrated above, with the following
additions:
setVal
method
print
method that prints the val
data member to cout
main
that tests your template, i.e., instantiates
several Simple
objects of different types and plays with them. (Lab 3.2.2 of CodeLab asks you to submit something
of this sort).
I've supplied you with the app code for the int
instantiation of Simple
— you can get it via the link at the bottom of the page.
Simple.h
,
main
) is that it should end in .cpp
(For example,
I used app.cpp
)
#include <Simple.h>
g++ *.cpp
or g++ app.cpp
(assuming your app was source file also named app.cpp
Simple
template
Working with int After initialization to 10 si: 10 After setVal(23) si: 23 Calling print: 23 Working with double After initialization to 10.5 sd: 10.5 After setVal(23.5) sd: 23.5 Calling print: 23.5 Working with string After initialization to "Hello" ss: Hello After setVal("Goodbye") ss: Goodbye Calling print: Goodbye
reverse
— accepts an array and its size, and reverses it (returns void
)
print
— accepts an array and its size, and prints it out in the form {e1, e2, ...., en-1}
(returns void
)
reverse.h
main
that tests your templates, i.e., calls them with
arrays of varying types. (Again as with the previous lab, Lab 3.3.2 of CodeLab asks you to submit something of this sort).
reverse.h
Code an app for the templates of 3.3.1, and which produces the following output: Playing with int Before: {10, 20, 30, 40, 50} After: {50, 40, 30, 20, 10} Playing with double Before: {10.5, 20.5, 30.5, 40.5, 50.5, 60.5} After: {60.5, 50.5, 40.5, 30.5, 20.5, 10.5} Playing with string Before: {ABC, DE, FGHI, JK} After: {JK, FGHI, DE, ABC}