C++ Programming Code Examples
C++ > Code Snippets Code Examples
Namespace Demo: define a namespace
/* Namespace Demo: define a namespace */
#include <iostream>
using namespace std;
namespace MyNameSpace {
class demo {
int i;
public:
demo(int x) {
i = x;
}
void seti(int x) {
i = x;
}
int geti() {
return i;
}
};
char str[] = "Illustrating namespaces\n";
int counter;
}
namespace SecondNamespace {
int x, y;
}
int main()
{
MyNameSpace::demo ob(10); // use scope resolution
cout << "Value of ob is : " << ob.geti();
cout << endl;
ob.seti(99);
cout << "Value of ob is now : " << ob.geti();
cout << endl;
using MyNameSpace::str; // bring str into current scope
cout << str;
using namespace MyNameSpace; // bring all of MyNameSpace into current scope
for(counter = 10; counter; counter--)
cout << counter << " ";
cout << endl;
SecondNamespace::x = 10; // use SecondNamespace namespace
SecondNamespace::y = 20;
cout << "x, y: " << SecondNamespace:: x;
cout << ", " << SecondNamespace::y << endl;
using namespace SecondNamespace; // bring another namespace into view
demo xob(x), yob(y);
cout << "xob, yob: " << xob.geti() << ", ";
cout << yob.geti() << endl;
return 0;
}
Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.
An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C++ programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number. C++ array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations.
A return statement ends the processing of the current function and returns control to the caller of the function. A value-returning function should include a return statement, containing an expression. If an expression is not given on a return statement in a function declared with a non-void return type, the compiler issues an error message. If the data type of the expression is different from the function return type, conversion of the return value takes place as if the value of the expression were assigned to an object with the same function return type.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:
In C++, constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C++ has the same name as class or structure. Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object. Whereas, Destructor on the other hand is used to destroy the class object. • Default Constructor: A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.
In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
This algorithm takes the input of the number of vertex. For each pair of vertex ask whether they're connected or not. Print the incidence matrix. Function to print the incidence matrix
If there exists "Multiple Strongly Connected" component, graph is not strongly connected, it is otherwise. Implementation of Kosaraju's Algorithm to "Print all SCCs". Fills Stack with
First 'Compare the Element' at the beginning with another array element sequentially. And swap values if the element at the beginning is Larger than the other element. This value will
Sort array of points according to X coordinate and Y coordinate. Find the distance between two points. And return the 'smallest distance' between 2 points. Find the 'distance' beween
To calculate "area and circumference" of any circle in "C++", you have to ask to the user to enter the radius of circle, place the radius in a variable say r and make two variable, one for