0% found this document useful (0 votes)
33 views40 pages

Relational, Logical and Bitwise Operators

This document contains code snippets demonstrating various C++ concepts like operators, functions, classes, inheritance, polymorphism, and exceptions. It includes examples of relational and bitwise operators, user defined functions to compare strings, multi-dimensional arrays, inline functions, classes and objects, friend functions, function overloading, inheritance, unary and binary operators, pure virtual functions, error handling, command line arguments, selection sort, manipulators, abstract classes and more. Each code example is accompanied by its output for demonstration purposes.

Uploaded by

ganapathy2010sv
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views40 pages

Relational, Logical and Bitwise Operators

This document contains code snippets demonstrating various C++ concepts like operators, functions, classes, inheritance, polymorphism, and exceptions. It includes examples of relational and bitwise operators, user defined functions to compare strings, multi-dimensional arrays, inline functions, classes and objects, friend functions, function overloading, inheritance, unary and binary operators, pure virtual functions, error handling, command line arguments, selection sort, manipulators, abstract classes and more. Each code example is accompanied by its output for demonstration purposes.

Uploaded by

ganapathy2010sv
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 40

RELATIONAL, LOGICAL AND BITWISE OPERATORS:

#include <iostream.h> #include <conio.h> int main() { unsigned short flags1; unsigned short flags2; flags1 = 0x00FF; flags2 = 0xFF00; clrscr(); cout << "Bitwise Operator: " << (flags1 & flags2) << endl; cout << "Logical Operator: " <<(flags1 && flags2) << endl; getch();

return 0; }

OUTPUT:
Bitwise Operator: 0 Logical Operator: 1

USER DEFINE FUNCTION TO COMPARE TWO STRINGS:


#include<iostream.h> #include<string.h> #include<conio.h> class kan { public: char str[30],str1[30]; void oper1() { cout<<"\nEnter the FIRST string :"; cin>>str; cout<<"\nEnter the SECOND string :"; cin>>str1; } void oper2() { if(strcmp(str,str1)==0) { cout<<"\nGiven string both are equal" ; }

else { cout<<"\nGiven string both are not equal"; } } }; void main() { clrscr(); kan s; s.oper1(); s.oper2(); getch(); }

OUTPUT:
Enter the FIRST string : C++ Enter the SECOND string : C++ Given string both are equal

Enter the FIRST string : C++ Enter the SECOND string : C Given string both are not equal

MULTI-DIMENSIONAL ARRAYS:
#include<iostream.h> #include<conio.h> #include<iomanip.h> int main() { const int Rows=4; const int Columns=4; int matrix[Rows][Columns]; int copyarray; const int End_of_row=4; const int End_of_col=4; int r; int c; clrscr(); cout<<"Enter the 16 intergers for data."<<endl; for(c=0;c<Columns;c++) matrix[Rows][Columns]=0; for(r=0;r<Rows;r++) matrix[Rows][Columns]=0; for(c=0;c<Columns;c++) for(r=0;r<Rows;r++)

cin>>matrix[r][c]; for(r=0;r<Rows;r++) { for(c=0;c<Columns;c++) cout<<setw(5)<<matrix[r][c]<<""; cout<<endl; ;} for(c=0;c<Columns;c++) { for(r=0;r<Rows;r++) ;} cout<<endl; for(r=0;r<Rows;r++) { for(c=0;c<Columns;c++) cout<<setw(3)<<matrix[c][r]<<""; cout<<endl; } return 0; getch(); }

OUTPUT:
Enter the 16 intergers for data. 1234567812345678 1 5 1 5 2 6 2 6 3 7 3 7 4 8 4 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8

INLINE FUNCTION:
#include<iostream.h> #include<conio.h> inline float triangle_area(float base,float height) { float area; area=(0.5*base*height); return area; } int main(void) { float b,h,a; b=4; h=6; a=triangle_area(b,h); clrscr(); cout<<"Area=(0.5*base*height)"<<endl; cout<<"Where,base=4,height=6"<<endl; cout<<"\n Area="<<a<<endl; return 0; getch(); }

OUTPUT:
Area=(0.5*base*height) Where,base=4,height=6 Area=12

CLASSES AND OBJECTS:


#include<iostream.h> #include<conio.h> class Box { public: double length; double breath; double height; }; int main() { clrscr(); Box Box1; Box Box2; double volume=0.0; Box1.height=5.0; Box1.length=6.0; Box1.breath=7.0; Box2.height=10.0; Box2.length=12.0; Box2.breath=13.0;

volume=Box1.height*Box1.length*Box1.breath; cout<<"Volume of Box1:"<<volume<<endl; volume=Box2.height*Box2.length*Box2.breath; cout<<"Volume of Box2:"<< volume<<endl; return 0; getch(); }

OUTPUT:
Volume of Box1:210 Volume of Box2:1560

FRIEND FUNCTION:
#include<iostream.h> #include<conio.h> class ONE; class TWO { public: void print(ONE&x); }; class ONE { int a,b; friend void TWO::print(ONE&x); public: ONE():a(1),b(2) { } }; void TWO::print(ONE&x) { cout<<"a is"<<x.a<<endl; cout<<"b is"<<x.b<<endl;

} int main() { clrscr(); ONE xobj; TWO yobj; yobj.print(xobj); return 0; getch(); }

OUTPUT:

a is 1 b is 2

FUNCTION OVERLOADING:
#include<iostream.h> #include<conio.h> long add(long,long); float add(float,float); int main() { long a,b,x; float c,d,y; clrscr(); cout<<"Enter two integers\n"; cin>>a>>b; x=add(a,b); cout<<"sum of integers:"<<x<<endl; cout<<"Enter two floating point numbers\n"; cin>>c>>d; y=add(c,d); cout<<"sum of floats:"<<y<<endl; return 0; } long add (long x,long y) {

long sum; sum=x+y; return sum; } float add(float x,float y) { float sum; sum=x+y; return sum; getch(); }

OUTPUT:

Enter two integers 4 9 sum of integers:13 Enter two floating point numbers 4.5 8.2 sum of floats:12.7

FORMS OF INHERITANCE:
#include<conio.h> #include <iostream.h> class Cpolygon { protected: int width, height; public: void input_values (int one, int two) { width=one; height=two; } };

class Crectangle: public Cpolygon { public: int area () { return (width * height); }

}; class Ctriangle: public Cpolygon { public: int area () { return (width * height / 2); } }; int main () { Crectangle rectangle; Ctriangle triangle; rectangle.input_values (2,2); triangle.input_values (2,2); clrscr(); cout<<"\n area of rectangle:"<<rectangle.area()<<endl; cout<<"\n area of triangle:"<<triangle.area()<<endl; return 0; getch(); }

OUTPUT: area of rectangle:4 area of triangle:2

UNARY AND BINARY OPERATORS:


#include<iostream.h> #include<conio.h> class Minus { private: int a, b, c ; public: Minus(int A, int B, int C) { a = A; b = B; c = C; } void display(void); void operator - ( ); }; void Minus :: display(void) { cout<<"\n\t a= " << a << endl ; cout<<"\n\t b= " << b << endl ; cout<<"\n\t c= " << c << endl ;

}inline void Minus :: operator - ( ) { a = -a ; b = -b ; c = -c ; } int main(void) { Minus M(5, 10, -15) ; Clrscr(); cout << "\nBefore activating operator - ( )" ; M.display( ) ; -M ; cout << "\nAfter activating operator - ( )" ; M.display( ) ; return 0; getch(); }

OUTPUT:
Before activating operator - ( ) a= 5 b= 10 c= -15 After activating operator - ( ) a= -5 b= -10 c= 15

PURE VIRTUAL FUNCTION:


#include <iostream.h> #include<conio.h> class Base { public: void Nonvirtual() { cout << "Base Nonvirtual called.\n"; } virtual voidvirtual() { cout << "Base virtual called.\n"; } }; class Derived : public Base { public: void Nonvirtual() { cout << "Derived Nonvirtual called.\n"; }

voidvirtual() { cout << "Derived virtual called.\n"; } }; int main() { clrscr(); Base* bBase = new Base(); Base* bDerived = new Derived(); bBase->Nonvirtual(); bBase->voidvirtual(); bDerived->Nonvirtual(); bDerived->voidvirtual(); getch(); }

OUTPUT:
Base Nonvirtual called. Base virtual called. Base Nonvirtual called. Derived virtual called.

ERROR HANDLING DURING FIVE OPERATIONS:


#include<iostream.h> #include<conio.h> int main() { double Operand1, Operand2, Result; char Operator; clrscr(); cout << "\n Enter the two number and its operator:"; cin >> Operand1 >> Operator >> Operand2; switch(Operator) { case '+': Result = Operand1 + Operand2; break; case '-': Result = Operand1 - Operand2; break; case '*': Result = Operand1 * Operand2; break; case '/':

Result = Operand1 / Operand2; break; default: cout << "Bad Operation"; } cout << "\n" << Operand1 << " " << Operator << " "<< Operand2 << " = " << Result; cout << "\n\n"; return 0; getch(); }

OUTPUT: Enter the two number and its operator: 30 + 30 30 + 30 = 60 Enter the two number and its operator: 30 - 30 30 - 30 = 0 Enter the two number and its operator: 30 * 30 30 * 30 = 900 Enter the two number and its operator: 30 / 30 30 / 30 = 1

COMMAND LINE ARGUMENTS TO PRINT THE GIVEN NUMBER IN DESCENDING AND ASCENDING ORDER:
#include <iostream.h> #include <stdlib.h> const int MAXSIZE = 10; void selectionSort(int arr[], int size); void swap(int& x, int& y); int main() { int numbers[] = { 13, 5, 1, 7, 9, 11, 3, 17, 19, 15 }; int k; cout << "BEFORE SORT: "; for (k = 0; k < MAXSIZE; k++) cout << numbers[k] << " "; selectionSort(numbers, MAXSIZE); cout << endl << endl; cout << "AFTER SORT: "; for (k = 0; k < MAXSIZE; k++) cout << numbers[k] << " "; cout << endl << endl << endl; system("PAUSE"); return 0;

} void selectionSort(int arr[], int size) { int indexOfMin, pass, j; for (pass = 0; pass < size - 1; pass++) { indexOfMin = pass; for (j = pass + 1; j < size; j++) if (arr[j] < arr[indexOfMin]) indexOfMin = j; swap(arr[pass], arr[indexOfMin]); } } void swap(int& x, int& y) { int temp; temp = x; x = y; y = temp; }

OUTPUT:
BEFORE SORT: 13 5 1 7 9 11 3 17 19 15 AFTER SORT: 1 3 5 7 9 11 13 15 17 19

MANIPULATORS:
#include <iostream.h> #include <stdlib.h> #include <iomanip.h> #include<conio.h> void main ( ) { float x = 23.012345678; float y = 44; float z = 3.765; float a = 4.5; clrscr(); cout << 2345.012345678 << endl; cout << 0.005544332211 << endl; cout << x << endl; cout << y << endl; cout << z << endl; cout << a << endl; cout << x << y << z << a << endl; system("PAUSE"); getch(); }

OUTPUT:
2345.012346 0.005544 23.012346 44 3.765 4.5 23.012346443.7654.5

ABSTRACT CLASS:
#include <iostream.h> #include <conio.h> class A { public: virtual void action() = 0; }; class B : public A { public: B() {} void action() { cout<<"\n Hello world!"; } }; class C : public A { public: C() {} void action()

{ cout<<"\n Good bye world!"; } }; class Aclass { public: void setinst(A *A_inst) { Apointer = A_inst; } void dosomething() { Apointer->action(); } private: A *Apointer; }; int main(int argc, char** argv) { clrscr(); Aclass A;

B Binst; C Cinst; A.setinst(&Binst); A.dosomething(); A.setinst(&Cinst); A.dosomething(); return 0; getch(); }

OUTPUT:
Hello world! Good bye world!

You might also like