Found 7347 Articles for C++

Compiling multiple .cpp files in c++ program

Anvi Jain
Updated on 30-Jul-2019 22:30:26

1K+ Views

Here we will see how to compile multiple cpp file in C++ program. The task is very simple. We can provide the names as a list to the g++ compiler to compile them into one executable fileTo compile multiple files like abc.cpp, and xyz.cpp at once, the syntax will be like this −g++ abc.cpp xyz.cppTo run the program, we can use this −./a.outExamplefloat area(float r){    return (3.1415*r*r); //area of a circle } float area(float l, float w) {    return (l * w); //area of a rectangle }Example#include #include "area.cpp" using namespace std; main() {    cout Read More

How to declare a global variable in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

2K+ Views

To declare global variables in C++, we can declare variables after starting the program. Not inside any function or block. If we want to declare some variables that will be stored in some different file, then we can create one file, and store some variable. For some external file sometimes we need to put the extern keyword with it. Also we have to include the external file with the actual program file.Exampleextern int x = 10; extern int y = 20;Example#include #include"global.cpp" using namespace std; int main() {    cout

What is the difference between a destructor and a free function in C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

452 Views

Here we will see what are the differences between destructor and the free() functions in C++. The destructor is used to perform some action, just before the object is destroyed. This action may not freeing up the memory, but can do some simple action such as displaying one message on screen.The free() function is used in C, in C++, we can do the same thing using delete keyword also. When the object is deleted using free() or delete, the destructor is invoked. The destructor function takes no argument and returns nothing. This function is called when free or delete is ... Read More

Enumerate over an enum in C++

Anvi Jain
Updated on 30-Jul-2019 22:30:26

307 Views

Enumeration is a user defined datatype in C/C++ language. It is used to assign names to the integral constants which makes a program easy to read and maintain. The keyword “enum” is used to declare an enumeration.The following is the syntax of enums.enum enum_name{const1, const2, ....... };Here, enum_name − Any name given by user. const1, const2 − These are values of type flag.The enum keyword is also used to define the variables of enum type. There are two ways to define the variables of enum type as follows −enum colors{red, black}; enum suit{heart, diamond=8, spade=3, club};Example#include using namespace std; ... Read More

What is difference between GCC and G++ Compilers?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

598 Views

We use gcc and g++ compilers in different times. Here we will see what are the differences between gcc and g++.The gcc is GNU C compiler, and g++ is GNU C++ compiler. The main differences are like below −gcc can compile *.c or *.cpp files as C and C++ respectivelyg++ can also compile *.c and *.cpp files, but take both as C++ fileIf we want to use g++ to link the object files, it automatically links in the STD C++ libraries. The gcc does not do thatgcc compiles C files which has fewer predefined macrosgcc compiles C++ files with more ... Read More

Compiling a C++ program with GCC

Anvi Jain
Updated on 14-Sep-2023 21:23:46

28K+ Views

Here we will see how to compile C++ program using GCC (GNU C Compiler). Let us consider, we want to compile this program.Example#include using namespace std; main() {    cout

How to prevent class inheritance in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

1K+ Views

Here we will see how to prevent inheritance in C++. The concept of preventing the inheritance is known as final class.In Java or C#, we can use final classes. In C++ there are no such direct way. Here we will see how to simulate the final class in C++.Here we will create one extra class called MakeFinalClass (its default constructor is private). This function is used to solve our purpose. The main Class MyClass can call the constructor of the MakeFinalClass as they are friend classes.One thing we have to notice, that the MakeFinalClass is also a virtual base class. ... Read More

What does it mean when a numeric constant in C/C++ is prefixed with a 0?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

122 Views

Sometimes we may see some numeric literals, which is prefixed with 0. This indicates that the number is octal number. So octal literals contain 0 at the beginning. For example, if an octal number is 25, then we have to write 025.Example#include int main() {    int a = 025;    int b = 063;    printf("Decimal of 25(Octal) is %d", a);    printf("Decimal of 63(Octal) is %d", b); }OutputDecimal of 25(Octal) is 21 Decimal of 63(Octal) is 51

Incompatibilities between C and C++

Smita Kapse
Updated on 30-Jul-2019 22:30:26

111 Views

Here we will see some incompatibilities between C and C++. Some C codes that can be compiled using C compiler, but does not compile in C++ compiler. And also returns error.We can define function using a syntax, that optionally specify the argument types after the argument list.Example#include void my_function(x, y)int x;int y; { // Not valid in C++    printf("x = %d, y = %d", x, y); } int main() {    my_function(10, 20); }Outputx = 10, y = 20OutputError in C++ :- x and y was not declared in this scopeIn C, or some older version of C++, the ... Read More

nextafter() and nexttoward() in C/C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

177 Views

Here we will see the effect of nextafter() and nextforward() functions in C or C++. These functions are present in the math.h or cmath library.if the functions are like nextafter(a, b) and nextforward(a, b). These functions are used to find the next representable value after a in the direction of b. The nextforward() has more precise second parameter b.Example#include #include main () {    //The nextafter function()    printf ("Smallest representable number after 0 towards 1 : %e", nextafter(0.0, 1.0));    printf ("Largest representable number before -1 towards 0 :%e", nextafter(0.0, -1.0));    printf ("Largest +ve representable number ... Read More

Advertisements