UrbanPro

Learn C Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

What are common bad practices beginners make with C++ header inclusions and file structures?

Asked by Last Modified  

Follow 1
Answer

Please enter your answer

Technical Trainer IT With Overall Experience of 10+ Years

This article is meant to address a common newbie problem regarding failure to understand #include, headers, and source file interaction. Several good practices are outlined and explained to show how to avoid some ugly snags. 1) Why we need header files. (1) It speeds up compile time. (2) It keeps...
read more
This article is meant to address a common newbie problem regarding failure to understand #include, headers, and source file interaction. Several good practices are outlined and explained to show how to avoid some ugly snags. 1) Why we need header files. (1) It speeds up compile time. (2) It keeps your code more organized (3) It allows you to separate interface from implementation. Those are the upsides, but the big, obvious downside is that is makes things a little more complicated if you don't understand how it all works. C++ programs are built in a two stage process. First, each source file is compiled on its own. The compiler generates intermediate files for each compiled source file. These intermediate files are often called object files -- but they are not to be confused with objects in your code. Once all the files have been individually compiled, it then links all the object files together, which generates the final binary (the program). This means that each source file is compiled separately from other source files. As a result of this, in terms of compiling, "a.cpp" is clueless as to what's going on inside of "b.cpp". Here's a quick example to illustrate: // in myclass.cpp class MyClass { public: void foo(); int bar; }; void MyClass::foo() { // do stuff } // in main.cpp int main() { MyClass a; // Compiler error: 'MyClass' is unidentified return 0; } Even though MyClass is declared in your program, it is not declared in main.cpp, and therefore when you compile main.cpp you get that error. This is where header files come in. Header files allow you to make the interface (in this case, the class MyClass) visible to other .cpp files, while keeping the implementation (in this case, MyClass's member function bodies) in its own .cpp file. That same example again, but tweaked slightly: // in myclass.h class MyClass { public: void foo(); int bar; }; // in myclass.cpp #include "myclass.h" void MyClass::foo() { } //in main.cpp #include "myclass.h" // defines MyClass int main() { MyClass a; // no longer produces an error, because MyClass is defined return 0; } The #include statement is basically like a copy/paste operation. The compiler will "replace" the #include line with the actual contents of the file you're including when it compiles the file. 2) What is the difference between .h/.cpp/.hpp/.cc/etc All files are fundamentally the same in that they're all text files, however different kinds of files should have different extensions: - Header files should use a .h__ extension (.h / .hpp / .hxx). Which of those you use doesn't matter. - C++ Source files should use a .c__ extention (.cpp / .cxx / .cc). Which of those you use doesn't matter. - C Source files should use .c (.c only). So what's the difference between Header files and Source files? Basically, header files are #included and not compiled, whereas source files are compiled and not #included. 3) The "right way" to include ** There are two basic kinds of dependencies you need to be aware of: 1) stuff that can be forward declared 2) stuff that needs to be #included If, for example, class A uses class B, then class B is one of class A's dependencies. Whether it can be forward declared or needs to be included depends on how B is used within A: - do nothing if: A makes no references at all to B - do nothing if: The only reference to B is in a friend declaration - forward declare B if: A contains a B pointer or reference: B* myb; - forward declare B if: one or more functions has a B object/pointer/reference as a parementer, or as a return type: B MyFunction(B myb); - #include "b.h" if: B is a parent class of A - #include "b.h" if: A contains a B object: B myb; read less
Comments

Related Questions

How are portions of a program disabled in demo versions?
By inserting comments in the program Single line comment denoted by // Multi line comment denoted by /* */
Akhilesh
0 0
5
Does a civil engineer need to learn the C language?
C language is a common subject in the first year of engineering irrespective of the branch. It may not be helpful for your civil engineering career but you definitely need to pass for B.Tech graduation.
Sandeep
0 0
5
Is the C language required to learn Java?
No, learning C is not a strict requirement for learning Java. Java and C are distinct programming languages with different syntax, features, and purposes. While having a background in C can provide a solid...
Akhilesh
0 0
7
How do I learn C programming by videos or books?
BOOKS AND PRACTICE by far remain the best way for learning any language ....
Fgrefg
0 0
7

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Memory Layout of C Programs
A typical memory representation of C program consists of following sections. Text Segment: A text segment, also known as a code segment or simply as text, is one of the sections of a program in an object...

C Program-Upper Case Demo
/*WAP to print the character entered by user in upper case*/ //Header files #include<stdio.h>#include<conio.h> //Main function void main(){ char ch; //Function for clearing screen clrscr(); ...

Tips of learning Java Language/Other Programming Languages
1.You should know the basic concept: If we talk about programming languages so basic concept are same in all the high level languages. So you should know the basic concept firstly then you can easily understand...
I

ICreative Solution

0 0
0

Bit wise operators in C
Bit Wise Operators Bit Wise operators are manipulates of individual bits with in a word of memory. The bit wise operators can be divided in to three general category. One’s Complement...

Variables
Variables in C Language:A variable is a name that may be used to store a data value. Unlike constant, variables are changeable, we can change value of a variable during execution of a program. A programmer...

Recommended Articles

Lasya Infotech is a Hyderabad based IT training institute founded in 2016 by O Venkat. Believing in his innovation, passion and persistence and with a diverse blend of experience, he started his brainchild to deliver exemplary professional courses to aspiring candidates by honing their skills. Ever since the institute envisions...

Read full article >

Brilliant Academy is one of the reputed institutes for B.Tech tuition classes. This institute is specialised in delivering quality tuition classes for B.E, Engineering - all streams and Engineering diploma courses. Incorporated in 2012, Brillant Academy is a brainchild of Mr Jagadeesh. The main motto of the academy is to...

Read full article >

Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...

Read full article >

Whether it was the Internet Era of 90s or the Big Data Era of today, Information Technology (IT) has given birth to several lucrative career options for many. Though there will not be a “significant" increase in demand for IT professionals in 2014 as compared to 2013, a “steady” demand for IT professionals is rest assured...

Read full article >

Looking for C Language Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for C Language Classes?

The best tutors for C Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C Language with the Best Tutors

The best Tutors for C Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more