C++ Programming Code Examples
C++ > Code Snippets Code Examples
Array class for 10 int value with overloaded functions: +, -, ==
/* Array class for 10 int value with overloaded functions: +, -, == */
#include <iostream>
using namespace std;
class array {
int nums[10];
public:
array();
void set(int n[10]);
void show();
array operator+(array object2);
array operator-(array object2);
int operator==(array object2);
};
array::array()
{
int i;
for(i = 0; i <10; i++) nums[ i ] = 0;
}
void array::set(int *n)
{
int i;
for(i = 0; i <10; i++) nums[ i ] = n[ i ];
}
void array::show()
{
int i;
for(i = 0; i <10; i++)
cout << nums[ i ] << ' ';
cout << endl;
}
array array::operator+(array object2)
{
int i;
array temp;
for(i = 0; i <10; i++)
temp.nums[ i ] = nums[ i ] + object2.nums[ i ];
return temp;
}
array array::operator-(array object2)
{
int i;
array temp;
for(i = 0; i <10; i++)
temp.nums[ i ] = nums[ i ] - object2.nums[ i ];
return temp;
}
int array::operator==(array object2)
{
int i;
for(i = 0; i <10; i++)
if(nums[ i ]!=object2.nums[ i ]) return 0;
return 1;
}
int main()
{
array object1, object2, object3;
int i[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
object1.set(i);
object2.set(i);
object3 = object1 + object2;
object3.show();
object3 = object1 - object3;
object3.show();
if(object1==object2)
cout << "object1 equals object2\n";
else
cout << "object1 does not equal object2\n";
if(object1==object3)
cout << "object1 equals object3\n";
else
cout << "object1 does not equal object3\n";
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.
In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.
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.
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.
#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.
n C++, we can change the way operators work for user-defined types like objects and structures. This is known as operator overloading. Suppose we have created three objects c1, c2 and result from a class named Complex that represents complex numbers. Since operator overloading allows us to change how operators work, we can redefine how the + operator works and use it to add the complex numbers of c1 and c2 by writing the following code:
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.
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.
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 function in C++ language is also known as procedure or subroutine in other programming languages. To perform any task, we can create function. A function can be called many times. It provides modularity and code reusability. Functions are used to provide modularity to a program. Creating an application using function makes it easier to understand, edit, check... Function declaration, is done to tell the compiler about the existence of the function. Function's return type, its name & parameter list is mentioned. Function body is written in its definition. Functions are called by their names. If the function is without argument, it can be called directly using its name. But for functions with arguments, we have two ways to call them: