CPP/Struct

From ProgrammingExamples
< CPP
Revision as of 00:15, 29 June 2010 by FirstPerson (Talk | contribs)

Jump to: navigation, search

Struct.cpp

#include <iostream>
#include <string>
 
using namespace std;
 
/*
 Use the keyword struct to declare a struct
 Syntax: struct structVariableName { content };
 
 Struct and classes are very similar in C++, the ONLY difference
 is that struct by default is set to public while class is set
 to private by default.
 
 Below shows an example of one of many usuage of struct. Examples are
 kept simple as possible.
*/
struct IntStringPair
{
private:
  int m_first;
  string m_second;
public:
  IntStringPair(const int intVal = 0, const string& strVal = "")
	  : m_first(intVal), m_second(strVal) 
  { 
  }
  int& first(){ return m_first; }
  string& second(){ return m_second; }
};
 
int main (){
	IntStringPair card = IntStringPair(10,"clubs");
	cout << card.first() << " of " << card.second() << endl;
  return 0;
}