Using a structure to define a class. : Struct Class « Class « C++






Using a structure to define a class.

Using a structure to define a class.
 


#include <iostream>
#include <cstring>
using namespace std;

struct StringStructClass {
  void initString(char *s); // public
  void displayString();
private:                  // private
  char str[255];
} ;

void StringStructClass::initString(char *s)
{
  if(!*s) 
      *str = '\0'; // initialize string
  else 
      strcat(str, s);
}

void StringStructClass::displayString()
{
  cout << str << endl;
}

int main()
{
  StringStructClass stringObject;

  stringObject.initString("");          
  stringObject.initString("Hello ");
  stringObject.initString("there!");

  stringObject.displayString();

  return 0;
}


           
         
  








Related examples in the same category

1.Constructor and destructor inside a structConstructor and destructor inside a struct
2.Stack class using a structure.Stack class using a structure.
3.Classes and Structures are Related
4.Using a class instead of struct.
5.add method to struct
6.use struct to initialize a class