How to create a simple class object in C++ programming stage 3, adding even 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 3, adding even more functionalities

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


// creating a very simple C++ Line class, step 3: adding even more functionalities

#include <iostream>

using namespace std;


// class, declaration

class line


char* color;

float weight;

float length;

char * arrow;



char* LineColor(char* color){return color;};

float LineWeight(float weight){return weight;};

float LineLength(float length){return length;};

char *LineArrow(char* arrow){return arrow = "YES";};




// implementation part



// constructors or initial values

weight = 0.25;

length = 10;




color = NULL;

weight = 0;

length = 0;

arrow = NULL;



// the main

void main(void)


line LineOne;

float x = 1.25, y = 2.25;

char newcolor[10] = "BLUE", *colorptr;


cout<<"Line attributes, a very simple C++ class example"<<endl;



colorptr = newcolor;


// just for testing the new attribute values

cout<<"\nAs normal variables....."<<endl;

cout<<"Test the new line weight = "<<x<<endl;

cout<<"Test the new line length = "<<y<<endl;

cout<<"Test the new line color is = "<<colorptr<<endl;

cout<<"\nUsing class......."<<endl;

cout<<"New line's color is "<<LineOne.LineColor(colorptr)<<endl;

cout<<"New line's weight is "<<LineOne.LineWeight(x)<<" unit"<<endl;

cout<<"New line's length is "<<LineOne.LineLength(y)<<" unit"<<endl;

cout<<"Line's arrow? "<<LineOne.LineArrow(" ")<<endl<<endl;





Output example:


Line attributes, a very simple C++ class example


As normal variables.....

Test the new line weight = 1.25

Test the new line length = 2.25

Test the new line color is = BLUE

Using class.......

New line's color is BLUE

New line's weight is 1.25 unit

New line's length is 2.25 unit

Line's arrow? YES

Press any key to continue . . .



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