C++ Programming Code Examples
C++ > Code Snippets Code Examples
Write a simple class called Cube.
/* Write a simple class called Cube.
The object should calculate the total area and the volume based on the side measurement.
If the program supplies a side equal or lower than 0, reset the side to 1.
Create an empty constructor and an empty destructor.
Implement the object in different files.
Header File: cube.h */
#ifndef CUBE_H
#define CUBE_H
class Cube
{
public:
Cube();
~Cube();
void setSide(double s);
double getSide();
double Area();
double Volume();
void Properties();
private:
double Side;
};
#endif
Source File cube.cpp
#include <iostream.h>
#include "cube.h"
Cube::Cube()
{
}
Cube::~Cube()
{
}
void Cube::setSide(double s)
{
Side = s <= 0 ? 1 : s;
}
double Cube::getSide()
{
return Side;
}
double Cube::Area()
{
return 6 * Side * Side;
}
double Cube::Volume()
{
return Side * Side * Side;
}
void Cube::Properties()
{
cout << "Characteristics of this cube";
cout << "\nSide = " << getSide();
cout << "\nArea = " << Area();
cout << "\nVolume = " << Volume() << "\n\n";
}
Main File: Exo.cpp
#include "cube.h"
void main()
{
Cube cube;
cube.setSide(-12.55);
cube.Properties();
Cube de;
de.setSide(28.15);
de.Properties();
}
Here is an example of the result:
Characteristics of this cube
Side = 1
Area = 6
Volume = 1
Characteristics of this cube
Side = 28.15
Area = 4754.53
Volume = 22306.7
The #ifndef directive of the C++ Programming Language helps in allowing the conditional compilation. The C++ Programming Language's preprocessor helps in determining only if the macro provided is not at all existed before including the specific subsequent code in the C++ compilation process. The #ifndef preprocessor only checks If the specific macro is not at all defined with the help of the #define directive. If the condition is TRUE then it will be helpful in executing the code otherwise the else code of the #ifndef will be compiled or executed only if present.
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.
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.
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.
#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 destructor is a special member function that works just opposite to constructor, unlike constructors that are used for initializing an object, destructors destroy (or delete) the object. Destructors in C++ are members functions in a class that delete an object. They are called when the class object goes out of scope such as when the function ends, the program ends, a delete variable is called etc. Destructors are different from normal member functions as they don't take any argument and don't return anything. Also, destructors have the same name as their class and their name is preceded by a tilde(~).
In the C++ Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions. The syntax for creating a constant using #define in the C++ is: #define token value
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.
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.
In C++ code, 'Declare and define' the function test(). Within the try block check whether the value is 'greater than zero' or not. If the value greater than zero 'throw the value' and catch
A "Recursive Function" to print 'DFS' starting from v. Returns reverse ('or transpose') of this graph. Add edge to connect v and w. Check if Graph is Connected and display 'The Graph is
Program has 'three functions' which receives 2 pointers reference. Three functions returns int, float and double sum of numbers. So this c++ tutorial use the following concepts. Write
Here, we use 'swapping' of the elements with the help of a Variable say temp of same type. That is, on found, start 'Swapping' with temp variable, place the first number in the temp &