Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Code Snippets Code Examples

Overloading Equality and Inequality Operators

/* Overloading Equality and Inequality Operators */ #include <iostream> using namespace std; class CDate { private: int m_nDay; int m_nMonth; int m_nYear; void AddDays (int nDaysToAdd); void AddMonths (int nMonthsToAdd); void AddYears (int m_nYearsToAdd); public: CDate (int nDay, int nMonth, int nYear) : m_nDay (nDay), m_nMonth (nMonth), m_nYear (nYear) {}; void DisplayDate () { cout << m_nDay << " / " << m_nMonth << " / " << m_nYear << endl; } // integer conversion operator operator int(); // equality operator that helps with: if (mDate1 == mDate2)... bool operator == (const CDate& mDateObj); // overloaded equality operator that helps with: if (mDate == nInteger) bool operator == (int nDateNumber); // inequality operator bool operator != (const CDate& mDateObj); // overloaded inequality operator for integer types bool operator != (int nDateNumber); }; CDate::operator int() { return ((m_nYear * 10000) + (m_nMonth * 100) + m_nDay); } // equality operator that helps with if (mDate1 == mDate2)... bool CDate::operator == (const CDate& mDateObj) { return ( (mDateObj.m_nYear == m_nYear) && (mDateObj.m_nMonth == m_nMonth) && (mDateObj.m_nDay == m_nDay) ); } bool CDate::operator == (int nDateNumber) { return nDateNumber == (int)*this; } // inequality operator bool CDate::operator != (const CDate& mDateObj) { return !(this->operator== (mDateObj)); } bool CDate::operator != (int nDateNumber) { return !(this->operator == (nDateNumber)); } void CDate::AddDays (int nDaysToAdd) { m_nDay += nDaysToAdd; if (m_nDay > 30) { AddMonths (m_nDay / 30); m_nDay %= 30; // rollover 30th -> 1st } } void CDate::AddMonths (int nMonthsToAdd) { m_nMonth += nMonthsToAdd; if (m_nMonth > 12) { AddYears (m_nMonth / 12); m_nMonth %= 12; // rollover dec -> jan } } void CDate::AddYears (int m_nYearsToAdd) { m_nYear += m_nYearsToAdd; } int main () { CDate mDate1 (25, 6, 2008); mDate1.DisplayDate (); CDate mDate2 (23, 5, 2009); mDate2.DisplayDate (); if (mDate2 != mDate1) cout << "The two dates are not equal... As expected!" << endl; CDate mDate3 (23, 5, 2009); mDate3.DisplayDate (); if (mDate3 == mDate2) cout << "mDate3 and mDate2 are evaluated as equals" << endl; // Get the integer equivalent of mDate3 using operator int() int nIntegerDate3 = mDate3; cout << nIntegerDate3<< endl; // Use overloaded operator== (for int comparison) if (mDate3 == nIntegerDate3) cout << "The integer and mDate3 are equivalent" << endl; // Use overloaded operator != that accepts integers if (mDate1 != nIntegerDate3) cout << "The mDate1 is inequal to mDate3"; return 0; }

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:

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:

Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:

#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.

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.

Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object. Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a this pointer. In C++ programming, this is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++: • It can be used to pass current object as a parameter to another method. • It can be used to refer current class instance variable. • It can be used to declare indexers. To understand 'this' pointer, it is important to know how objects look at functions and data members of a class.

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.

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 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.

The pointer in C++ language is a variable, it is also known as locator or indicator that points to an address of a value. In C++, a pointer refers to a variable that holds the address of another variable. Like regular variables, pointers have a data type. For example, a pointer of type integer can hold the address of a variable of type integer. A pointer of character type can hold the address of a variable of character type. You should see a pointer as a symbolic representation of a memory address. With pointers, programs can simulate call-by-reference. They can also create and manipulate dynamic data structures. In C++, a pointer variable refers to a variable pointing to a specific address in a memory pointed by another variable.



Program demonstrates the implementation of Randomized Binary Search Tree. Function to check if tree is empty. And then make the tree logically empty. Functions to insert data.

It initializes the value current level, permutes the remaining values to the 'higher levels'. As the Assigning Action of the values reaches to the "Highest Level", it prints the permutation





Takes the input of the number of 'vertexes' & the number of edges in the graph. It takes the input of vertex pairs for the given number of edges. It generates a line graph for the given