Labels

Android (1) bash (2) boost (2) C (34) C++ (2) cheatsheet (2) CLion (6) css (3) Debian (33) DL (17) Docker (1) Dreamweaver (2) Eclipse (3) fail2ban (4) git (5) GitHub (4) Hacking (3) html (8) http (1) iOS (1) iPad (1) IRC (1) Java (30) javascript (3) Linux (164) Mac (19) Machine Learning (1) mySQL (47) Netbeans (4) Networking (1) Nexus (1) OpenVMS (6) Oracle (1) Pandas (3) php (16) Postgresql (8) Python (9) raid (1) RedHat (14) Samba (2) Slackware (45) SQL (14) svn (1) tar (1) ThinkPad (1) Virtualbox (3) Visual Basic (1) Visual Studio (1) Windows (2)

Saturday 6 June 2020

Examples of basic C++ Header and class files.

Example of basic Header and class files

1 - Example of a basic Header File


see: https://github.com/plisken1/CPP_HeaderExample

Here we have two files;

#############################################################################

CPP_HeaderExample.cpp

#include <iostream>
#include "Utilities.h"
using namespace std;


int main()
{
string name = getInput("What is your name?");
print( "Your name is "+name,true);
return 0;
}

#############################################################################

And

#############################################################################

Utilities.h

#include <iostream>
using namespace std;

string getInput(string text, bool newLine=false)
{
string answer;
if (newLine)
{
cout << text<<endl;
}
else
{
cout << text;
}
cin >> answer;
return answer;
}
void print(string text, bool newLine=false)
{
if (newLine)
{
cout << text<< endl;
}
else
{
cout << text;
}
}

#############################################################################

What we have here is the test.cpp which is including or importing a Header file, Utilities.h which resides in the same directory. The header file contains two functions, getInput and print and these are used in the text.cpp file.

#############################################################################
#############################################################################

2 - Example of a basic Class file (single file)

See https://github.com/plisken1/CPP_ClassExample 

In this first example below, the class is defined in  the same file;
#############################################################################

#include <iostream>
using namespace std;

class MyClass
{
private: // Not actually required.
    int privateInt;

public:
    int myNum; // Class variable
    string myString; // Class variable

    int getPrivateInt() // Public GETTER method.
    {
    return privateInt;
    }

    void setPrivateInt(int anInt) // Public SETTER method.
    {
    privateInt=anInt;
    }

    int doubleMe(int anInt)
    {
    return anInt*2;
    }
};

// Main part of program.
int main()
{
MyClass o; // Create object of MyClass


  o.myNum = 15; // Assign variable to object.
  o.myString = "Some text"; // Assign variable to object.
  o.setPrivateInt(5);

cout << o.myNum << endl;
cout << o.myString << endl;
cout << o.getPrivateInt() << endl;
cout << o.doubleMe(50) << endl;

return 0;
}

#############################################################################
#############################################################################

3 - Example of a basic Class file (separate file)

In the second example below, there are 3 files required, the cpp file and 2 files associated with the class, a cpp and a header file.

#############################################################################

MyOtherClass.cpp - The Class file file.

#include "MyOtherClass.h"
#include<iostream>

std::string myString;
std::string a;

MyOtherClass::MyOtherClass()
{
// TODO Auto-generated constructor stub
}

std::string MyOtherClass::getString()
{
return myString;
}
void MyOtherClass::setString(std::string aStr)
{
myString=aStr;
}


MyOtherClass::~MyOtherClass()
{
// TODO Auto-generated destructor stub
}

#############################################################################

MyOtherClass.h - The Header file

#ifndef MYOTHERCLASS_H_
#define MYOTHERCLASS_H_
#include<iostream>

class MyOtherClass
{
public:
MyOtherClass();
std::string getString(); // Added
void setString(std::string); // Added
std::string a; // Added
virtual ~MyOtherClass();

};

// TODO Add GETTER and SETTER methods


#endif /* MYOTHERCLASS_H_ */

#############################################################################

CPP_ClassExample.cpp - The main file.

#include <iostream>
#include "MyOtherClass.h"
using namespace std;



// Main part of program.
int main()
{

MyOtherClass oc; // Create new object of MyOtherClass


  // MyOtherClass
  oc.a="good day";         // Assign variable to MyOtherClass object.
  oc.setString("ok then"); // Call SETTER Method.


// MyOtherClass
cout << oc.a << endl;
cout << oc.getString() << endl; // MyOtherClass GETTER method.

return 0;
}

#############################################################################

No comments:

Post a Comment

Note: only a member of this blog may post a comment.