Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Do coding standards exist for C++? 2.Consistency is one key piece that helps

ID: 3850101 • Letter: 1

Question

1. Do coding standards exist for C++?

2.Consistency is one key piece that helps make code readable. Unfortunately, even though you can get programmers to agree up front on a particular style, you will find it doesn't last. Most programs hang around for years with people changing jobs and programs they maintain. Therefore, you tend to get different hands into the pot. Not everyone uses the same style. Where I work, we adhere to a programming standard that requires us to stay with the original author's style unless we rewrite the file completely.

One of the things that makes code readable is the format of variable names. One format used is Hungarian notation (e.g., m_pExample, g_pExample). Bjarne Stroustrup has his own view on that topic and other naming conventions: http://www.stroustrup.com/bs_faq2.html#Hungarian . Does anyone in class agree with the man who designed C++? If not, why would your opinion differ? Any comments on his preferences for naming conventions?

3.I have a number of projects going on at work and with many of them I use Git. I have also used Subversion (SVN) and CVS. The wonderful thing about it is that these tools are free (gotta love open source).

The repository where we keep the code is on a Linux server (free) and the clients I use to check in/out code are also free. In Windows I use Git Bash, a command line tool, but I have also used a nice graphical version called Tortoise (for SVN). It plugs into the Windows shell so part of the interface works through Windows Explorer.

When speaking of revision control, what is the Head and what is a Branch?

4.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 <iostream>

void ex1::print()
{
...std::cout<<"Hello"<<std::endl;
}

------------------------------------------------------------------

//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?

Explanation / Answer

1. Yes coding standards do exist for C++. They include the naming convention for the variables, syntax styles, if else constructs etc.

2. The naming convention for the variables is very important in the context of large programs, where you may have one global varible defined in header file which you will be using in your source file. if the variable is prefixed with 'g_', it gives a intant idea that the variable is a global variable and need to be searched for in the header file. Hence, yes the variable naming conventiones should be followed. The abbreation can be the cases where programs are very small or you use a local variable in a block such as loop index.. there having i as variable name is fine, because scope is very small.

3. Branch is known as a seperate repository which contains code till now and if we merge on that branch, the code will not mix to master branch. Initially you have only one branch which is known as Master branch.
The head is known as the current branch on which you are working.

4. The #pragma once are defined to avoid multiple declarations of the same thing, as the compiler gives error on mulitple declarations. As you know, the programmers directly use the header file with include construct in their source code. What if the source code of 2 files, which will be compiled together, contains reference of same header file twice. In that case, if there is some class definintion or global variable in the class, that gets defined multiple times, which is a compile time error. Hence using the pragma, we only define the class once.