C++ Programming Code Examples
C++ > Algorithms Code Examples
Rational Mini Project. About overloading of operators
/* Rational Mini Project. About overloading of operators */
//main subject:operator-overloading
#include <iostream.h>
class rational
{
private:
int a;
int b;
public:
rational(int=1,int=1);
void set_ab(int,int);
void print_fraction();
void print_floating();
rational operator+(rational object);
rational operator-(rational object);
rational operator*(rational object);
rational operator/(rational object);
rational operator=(rational object);
friend ostream &operator<<(ostream &,rational &);
friend istream &operator>>(istream &,rational &);
int operator==(rational object);
int operator!=(rational object);
int operator>(rational object);
int operator<(rational object);
};
/*--------------------------------------------------------*/
rational::rational(int m,int n)
{
set_ab(m,n);
}
/*--------------------------------------------------------*/
void rational::set_ab(int x,int y)
{
int temporary,m,n;
m=x;
n=y;
if(n>m)
{
temporary=n;
n=m;
m=temporary;
}
while(m!=0 && n!=0)
{
if(m%n==0)
break;
temporary=m%n;
m=n;
n=temporary;
continue;
}
a=x/n;
b=y/n;
}
/*--------------------------------------------------------*/
void rational::print_fraction()
{
cout<<a<<"/"<<b<<"=";
}
/*--------------------------------------------------------*/
void rational::print_floating()
{
cout<<(float)a/b<<endl;
}
/*--------------------------------------------------------*/
rational rational::operator+(rational object)
{
rational temporary;
temporary.a=a*object.b+b*object.a;
temporary.b=b*object.b;
return(temporary);
}
/*--------------------------------------------------------*/
rational rational::operator-(rational object)
{
rational temporary;
temporary.a=a*object.b-b*object.a;
temporary.b=b*object.b;
return(temporary);
}
/*--------------------------------------------------------*/
rational rational::operator*(rational object)
{
rational temporary;
temporary.a=a*object.a;
temporary.b=b*object.b;
return(temporary);
}
/*--------------------------------------------------------*/
/*
rational rational::operator/(rational object)
{
rational temporary;
temporary.a=a*object.b;
temporary.b=b*object.a;
return(temporary);
}
*/
/*--------------------------------------------------------*/
rational rational::operator/(rational object)
{
a=a*object.b;
b=b*object.a;
return(*this);
}
/*--------------------------------------------------------*/
rational rational::operator=(rational object)
{
a=object.a;
b=object.b;
return(*this);
}
/*--------------------------------------------------------*/
ostream &operator<<(ostream &output,rational &object)
{
output<<object.a<<"/"<<object.b<<endl;
return output;
}
/*--------------------------------------------------------*/
istream &operator>>(istream &input,rational &object)
{
cout<<"Enter a,b:"<<endl;
input>>object.a;
input>>object.b;
return input;
}
/*--------------------------------------------------------*/
int rational::operator==(rational object)
{
if(a==object.a && b==object.b)
return(1);
else
return(0);
}
/*--------------------------------------------------------*/
int rational::operator!=(rational object)
{
if(a!=object.a || b!=object.b)
return(1);
else
return(0);
}
/*--------------------------------------------------------*/
int rational::operator>(rational object)
{
return((a/b)>(object.a/object.b) ? 1:0);
}
/*--------------------------------------------------------*/
int rational::operator<(rational object)
{
return((a/b)<(object.a/object.b) ? 1:0);
}
/*--------------------------------------------------------*/
int main()
{
rational x(3,4),y(3,4),z1,z2,z3,z4,z5;
if(x==y)
{cout<<"These 2 fractions are equl."<<endl;}
if(x!=y)
{cout<<"These 2 fractions are not equl."<<endl;}
if(x>y)
{cout<<"x is bigger than y."<<endl; }
if(x<y)
{cout<<"x is smaller than y."<<endl;}
z1=x+y;
cout<<"z1=x+y=";
z1.print_fraction();
z1.print_floating();
z2=x-y;
cout<<"z2=x-y=";
z2.print_fraction();
z2.print_floating();
z3=x*y;
cout<<"z3=x*y=";
z3.print_fraction();
z3.print_floating();
z4=x/y;
cout<<"z4=x/y=";
z4.print_fraction();
z4.print_floating();
z5=y=x;
cout<<"x=";
x.print_fraction();
x.print_floating();
cout<<"y=";
y.print_fraction();
y.print_floating();
cout<<"z5=";
z5.print_fraction();
z5.print_floating();
/* rational k(1,1),l(1,1),z6,z7,z8,z9,z10;
cin>>k>>l;
if(k==l)
{cout<<"These 2 fractions are equl."<<endl;}
if(k!=l)
{cout<<"These 2 fractions are not equl."<<endl;}
if(k>l)
{cout<<"k is bigger than l."<<endl;}
if(k<l)
{cout<<"k is smaller than l."<<endl;}
z6=k+l;
z7=k-l;
z8=k*l;
z9=k/l;
z10=l=k;
cout<<"z6="<<z6<<"z7="<<z7<<"z8="<<z8<<"z9="<<z9;
cout<<"k="<<k<<"l="<<l<<"z10="<<z10;
*/
return(0);
}
Continue statement is used inside loops. Whenever a continue statement is encountered inside a loop, control directly jumps to the beginning of the loop for next iteration, skipping the execution of statements inside loop's body for the current iteration. The continue statement works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between. For the for loop, continue causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, program control passes to the conditional tests.
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 cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.
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:
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.
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.
Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.
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.
A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions. A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends. If a function is defined as a friend function in C++ programming language, then the protected and private data of a class can be accessed using the function. By using the keyword friend compiler knows the given function is a friend function. For accessing the data, the declaration of a friend function should be done inside the body of a class starting with the keyword friend.
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
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.
A predefined object of the class called iostream class is used to insert the new line characters while flushing the stream is called endl in C++. This endl is similar to \n which performs the functionality of inserting new line characters but it does not flush the stream whereas endl does the job of inserting the new line characters while flushing the stream. Hence the statement cout<<endl; will be equal to the statement cout<< '\n' << flush; meaning the new line character used along with flush explicitly becomes equivalent to the endl statement in C++.
In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.
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:
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.
#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.
Write a 'C++' program which Counts numbers of vowels in a given string and tell every index where a 'Vowel is Found'. Size of array is fixed using the constant variable. We enter a string
If graph has no "Odd Degree Vertex", there is at least one "Eulerian Circuit". If graph as two vertices with odd degree, there is no Eulerian Circuit but at least one Eulerian Path. If graph