CODE FOR + OPERATOR OVER LOADING C++

 PROBLEM STATEMENT: ADD 2 OBJECTS AND ADD INTO OTHER. CODE: #include<iostream> using namespace std; class Experience { private: int days; int month; double year; public: Experience() { } Experience(int a,int b,double c) { days = a; month = b; year = c; } Experience operator +(Experience E) { Experience E1; E1.days = days + E.days; E1.month = month + E.month; E1.year = year + E.year; return E1; } void display() { cout << "\nDAYS\t" << days << "\nMONTHS\t" << month << "\nYEAR\t" << year << endl; } }; int main() { Experience e1(2, 12, 2), e2(1, 12, 4), e3; e3 = e1 + e2; cout << "showing data of 1st Experience\n"; e1.display(); cout << "\n\n\n\n"; cout << "showing data of 2nd Experience\n"; e2.display(); cout << "\n\n\n\n"; cout << "showing data of sum of Experiences\

LOOPS IN C++(table making in c++)

LOOPS IN C++



why we use loops in c++?
what loops do in c++?
what is function of  loops c++?
In this blog I will share you interesting information on loops in c++.
lets start,
In c++ loops are required to do repetative tasks like to enter data of 50 or 1000 employees ,to order one or more items in same program.
without using loops it is impossible and difficult to enter data of different people in one variable or to write your name in required time,to solve this issue loopsare used in C++
There are three types of loops in C++  with different syntax i.e: 

  • FOR LOOPS 
  • WHILE LOOPS
  • DO WHILE LOOPS

 Each loop do same function but here is syntax difference between them as shown in following figure:
lets make a random table program using c++:

  • Using For loop:        

#include<iostream>

using namespace std;

int main()

{

int a;

cout << "enter no\n";//\n is for line break 

cin >> a;//taking input from user

for (int i = 1; i <= 10; i++)//we initialized variable in for loop

{

cout << a  << '*' << i << "\t" << '=' << a * i << endl;

}//here \t is for space 

}    

OUTPUT:

                    

















  • USING WHILE LOOP:

#include<iostream>

using namespace std;

int main()

{

int a;

int i = 1;

cout << "enter no\n";//\n is for line break 

cin >> a;//taking input from user

while(i<=10)

{

cout << a  << '*' << i << "\t" << '=' << a * i << endl;

i++;

}//here \t is for space 

}

Output:


  • USING DO WHILE LOOP

#include<iostream>

using namespace std;

int main()

{

int a;

int i = 1;

cout << "enter no\n";//\n is for line break 

cin >> a;//taking input from user

do

{

cout << a  << '*' << i << "\t" << '=' << a * i << endl;

i++;

} while (i <= 10);//here \t is for space 

}

output:

FOR MORE INFORMATION WATCH:

Comments

Post a Comment

Popular posts from this blog

CODE FOR CAR AND BIKE MANAGEMENT USING(Virtual functions) C++

NESTED LOOPS IN C++