How to create a simple class object in C++ programming stage 2, adding more functionalities

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Creating and tracking the class execution (constructor and destructor) order in C++ programming stage 2, adding more functionalities

To show: How to create and use a simple class in C++ programming, stage 2, adding more functionalities

 

// creating a simple C++ class, step 2: adding more functionalities

#include <iostream>

using namespace std;

 

// class, declaration

class line

{

char* color;

int pattern;

public:

line(void);

char* LineColor(char*){ return color = "GREEN";};

int LinePattern(int pattern){return pattern;};

~line(void);

};

 

// class implementation

line::line(void)

{

// constructor/default value

pattern = 12;

}

line::~line(void)

{

color = NULL;

pattern = 0;

}

 

// main program

void main(void)

{

line LineOne;

int x = 10;

 

cout<<"A simple class of line: using some user supplied value"<<endl;

cout<<"---------------------------------------------------"<<endl;

cout<<"Line's Color is "<<LineOne.LineColor("")<<endl;

cout<<"Line's pattern type is "<<LineOne.LinePattern(x)<<endl;

 

return;

}

 

Output example:

 

A simple class of line: using some user supplied value

---------------------------------------------------

Line's Color is GREEN

Line's pattern type is 10

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index