+ All Categories
Home > Technology > Object Oriented Programming with C++

Object Oriented Programming with C++

Date post: 11-Apr-2017
Category:
Upload: golden-sunshine
View: 21 times
Download: 0 times
Share this document with a friend
22
Object Oriented Programming With C++
Transcript
Page 1: Object Oriented Programming with C++

Object Oriented Programming With C++

Page 2: Object Oriented Programming with C++

void Swap(int a,int b) { a=a+b; b=a-b; a=a-b; cout<<a; cout<<b; }

Q.1 Swap two number     without using third variable ?

Page 3: Object Oriented Programming with C++

void main() {  clrscr(); int a,b; cout<<"Enter two numbers:"; cin>>a>>b; Swap(a,b); getch(); }

Page 4: Object Oriented Programming with C++

Output: enter two numbers 10 12 12 10

Page 5: Object Oriented Programming with C++

Write a C++ program to perform Unary Operator Overloading on minus('-') operator. Ans:      #include<iostream.h>              #include<conio.h>              class abc              {               public:               int a,b,c;

Page 6: Object Oriented Programming with C++

abc() {   } abc(int x,int y,int z) { a=x; b=y; c=z; }

Page 7: Object Oriented Programming with C++

void display() { cout<<a; cout<<b; cout<<c; } void  operator -() { a=-a;

Page 8: Object Oriented Programming with C++

b=-b; c=-c; } }; void main() { clrscr(); abc a1(10,20,30); a1.display();

Page 9: Object Oriented Programming with C++

-a1; a1.display(); getch(); } Output: 10               20               30               -10               -20               -30

Page 10: Object Oriented Programming with C++

Q.2 Write a C++ program operator                overloading for operator '++' to            increase the value for object        using member function. Ans: # include<iostream.h>         # include<conio.h>         class abc         {          int a,b,c;  

Page 11: Object Oriented Programming with C++

abc(); {    } abc ( int x,int y,int z) { a=x; b=y; c=z; }

Page 12: Object Oriented Programming with C++

void display() { cout<<a; cout<<b; cout<<c; } void operator ++() {

Page 13: Object Oriented Programming with C++

void display() { cout<<a; cout<<b; cout<<c; } void operator ++() {

Page 14: Object Oriented Programming with C++

a= ++a; b= ++b; c= ++c; } }; void main() { clrscr();

Page 15: Object Oriented Programming with C++

abc a1(10,20,30); a1.display(); ++a1; a1.display(); getch(); } Output:               10 20 30               11  21  31

Page 16: Object Oriented Programming with C++

Write a C++ program using Unary operator using friend function Ans:         # include <iostream.h>         # include <conio.h>          class inc          {           public:

Page 17: Object Oriented Programming with C++

int a,b,c; public: inc () {   } inc (int x,int y,int z) { a=x; b=y;

Page 18: Object Oriented Programming with C++

c= z; } void display() { cout<<a; cout<<b; cout<<c; }

Page 19: Object Oriented Programming with C++

friend void operator +(inc &i); }; void operator +(inc &i) { i.a=+i.a; cout<<i.a; i.b=+i.b; cout<<i.b;

Page 20: Object Oriented Programming with C++

i.c=+i.c; cout<<i.c; } void main() { clrscr(); inc.c1(10,20,30); inc.c2;

Page 21: Object Oriented Programming with C++

c1.display(); c2+c1; c2.display(); c1.display(); getch(); }

Page 22: Object Oriented Programming with C++

output: 10               20               30               20              40              60

        garbage value            10            20            30


Recommended