Data Types, functions and a basic class example

Learning a programming language is hard enough without having to wrangle your computer into compiling and executing your code. From beginners to experts, the tips here will make life in C++ easier.
Post Reply
darknkreepy3#
Site Admin
Posts: 247
Joined: Tue Oct 27, 2009 9:33 pm

Data Types, functions and a basic class example

Post by darknkreepy3# »

Code: Select all

#include <iostream>
#include <string>

using namespace std;
//-----
class CalculateAnswer
     {
     public:
          CalculateAnswer();
          ~CalculateAnswer();
          float findScore(float sentCorrect, float sentTotal);
     private:
          float theScore;
	};
CalculateAnswer::CalculateAnswer()
     {
     }
CalculateAnswer::~CalculateAnswer()
     {
     }	
float CalculateAnswer::findScore(float sentCorrect,float sentTotal)
     {
     theScore=sentCorrect/sentTotal;
     return theScore;
	 }     
//-----
float scoreTotal(float sentCorrect, float sentTotal)
     {
     float scoreVal=sentCorrect/sentTotal;
     return scoreVal;
	}
//-----
int main(int argc, char *argv[])
{
     int age=35;
     cout << "My age is " << age << endl;
     //
	 float score;
     float correct=7;
     float total=11;
     score=correct/total;
     cout << "A floating point of your current score is " << correct << "\\" << total << "=" << score << endl;
     //you need the <string> include for this
	 string worker="kim";
     cout << "Is your name " << worker << "?" << endl;
     //you can send info to the standalone function 'scoreTotal' and get a response inline your cout statement
	 cout << "Another way to find the score is to put it in a function.  Score is " << scoreTotal(correct,total) << endl;
     //you can create a new object instance of the 'CalculateAnswer' class, and call it 'mathGrade'
     CalculateAnswer mathGrade;
     /*
	 then you can ask that instance to run its method, or public function 'findScore' with a dot(.) and the variables 'correct' and 'total'
     if both of those variables are of the same type 'float' as the function requests in it's definition 'CalculateAnswer::findScore(.....)
     and in the class public definition 'findScore(.....)', all types of the declaration, definition, and instance calling to run should all
     be of the same type per variable position
	 */
     cout << "You can also ask a function inside of a class instance the same info: " << mathGrade.findScore(correct,total) << endl;
     //
     system("PAUSE");
     return EXIT_SUCCESS;
}
Post Reply