Class declarations and definitions

0. A declaration which reserves memory is more properly known as a definition in C++.

1. Class declarations is different than class definitions in C++.

2. Class declaration is what you do in a header file. The “class declaration block” consists of the keyword class, followed by the name of the object, a pair of braces, and a semi-colon. It basically declares class interface, with function prototypes and static constants.

3. Non constant static members (i.e., static class variables) need to be declared but not initialized, inside a class (header), like so:

static int totalStudents;

The member has to initialized outside the class (header) and inside a class instance definition (i.e., the source code file, *.cpp), like so:

int GradeBook::totalStudents = 10;

4. When you initializing a class instance whose constructor takes no parameters, you do it like this:

GradeBook gb;

as opposed to

GradeBook gb();

But if the constructor does take parameters, you do it like this:

GradeBook gb("C++ Programming");

Example:
// GradeBook2.h
#include
class GradeBook
{
public:
GradeBook(bool foo);
GradeBook();
~GradeBook();
void countGrades();
void displayResults();
//const int TEST = 12; //wrong.
static int totalStudents; // non constant static member needs to be declared inside class here.
static const int TOTAL_STUDENTS_SCHOOL = 200; //constants has to be declared and initialized here inside class.s
//static const int TOTAL; // wrong. will compile but get runtime error whenever this member data is referred. "undefined reference to `GradeBook::TOTAL'"
private:
int passedTotal;
int failedTotal;
};


// GradeBook2.cpp
#include "gradebook2.h"
#include <iostream>;
using namespace std;

int GradeBook::totalStudents = 10;
//int GradeBook::TOTAL = 300; //error: declaration of ‘const int GradeBook::TOTAL’ outside of class is not definition

GradeBook::GradeBook(bool foo)
{
cout<<“you said it’s “<<<“\n”;
countGrades();
};

GradeBook::GradeBook()
{
cout<<“you did NOT say anything\n”;
countGrades();
};

GradeBook::~GradeBook()
{
cout<<“bye bye!\n”;
};

void GradeBook::countGrades()
{
cout<<“this is a gradebook! and we have a total of “<< TOTAL_STUDENTS_SCHOOL <<” students in school!\n”;
//cout<<“the total is “<< TOTAL <<” though!\n”;
//cout<<“Total students in the book are “<<<“/n”;//test
}

void GradeBook::displayResults()
{
}


// runner.cpp
#include "gradebook2.cpp"
#include <iostream>;
using namespace std;

int main()
{
//int GradeBook::totalStudents;//error: invalid use of qualified-name ‘GradeBook::totalStudents’
GradeBook gb(true);
GradeBook gb2;
}

Some links:

C++ static varialbles

Declaring a class

Class declaration vs. class definition

The C++ ‘const’ Declaration: Why & How

How to declare/initialize a constant in a class?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: