Here is an example of how you separate your code into different files add the he
ID: 3849142 • Letter: H
Question
Here is an example of how you separate your code into different files add the header file and the .cpp file to the project.
------------------------------------------------------------------
//Header file -- saved as ex1.hpp
#pragma once
class ex1
{
public:
...ex1(){}
...void print();
};
------------------------------------------------------------------
//.cpp file -- saved as ex1.cpp
#include "ex1.hpp"
#include
void ex1::print()
{
...std::cout<<"Hello"< }
------------------------------------------------------------------
//main -- saved as main.cpp
#include "ex1.hpp"
int main(void)
{
...ex1 e();
...e.print();
...return 0;
}
------------------------------------------------------------------
Why do I have the #pragma once in the .hpp file?
second question
I have seen my share of x,n and i variable names. My general rule of thumb is that the length of a variable name is proportional to the length of it's scope. I.e., big function = big variable name.
Would you agree?
Explanation / Answer
First Question:
The reason why #pragma once is used in the header file(.hpp) is simply to perform the check if the folowing part is defined in the program earlier by any other included header file or not. It will include the contents of your header file only if they are new i.e they are not already included in your program
Second Question:
You have got quite the view on the length of variable names. It is not mandatory to name your variables according to their scope but it is considered to be good practice to create variable name according to their scopes as the global variables generally have variable names in uppercase and local variables are in lowercase. The variable names must be meaningful so if we are using a variable more than once in our program then we can not assign it as x, i, j,k etc. (although we can) as they does not make any sense .Therefore variables with single letter are used in local scope where the scope of variable is so small that it does not have to have a meaningful name , it is understood by the programmer.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.