#include<iostream.h> #include<iostream.h> class Buah { private: int apel; int jeruk; public: Buah(int jum_apel=0,int jum_jeruk=0); void info_buah(); Buah operator+(Buah b2); Buah operator+(int tambahan); }; void main() { clrscr(); Buah buah1 (20,5); Buah buah2; cout<} Buah Buah::operator+(Buah b2) { Buah tmp; tmp.apel=apel+b2.apel; tmp.jeruk=jeruk+b2.jeruk; return(tmp); } Buah Buah::operator+(int tambahan) { Buah tmp; tmp.apel=apel+tambahan; tmp.jeruk=jeruk+tambahan; return(tmp); }<
Jumat, 08 Juli 2011
Operator overloading,operator tambah C++
Berikut ini adalah salah satu contoh program overloading operator tambah dalam bahasa C++.
This Example Programs Calculate Prime Number Using Constructor
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class as Prime with data members,
Member functions.
STEP 3: Consider the argument constructor Prime() with integer
Argument.
STEP 4: To cal the function calculate() and do the following steps.
STEP 5: For i=2 to a/2 do
STEP 6: Check if a%i==0 then set k=0 and break.
STEP 7: Else set k value as 1.
STEP 8: Increment the value i as 1.
STEP 9: Check whether the k value is 1 or 0.
STEP 10:If it is 1 then display the value is a prime number.
STEP 11:Else display the value is not prime.
STEP 12:Stop the program.
C++ PROGRAM:
Sample Output:
Enter the number: 7
Given number is Prime Number
STEP 1: Start the program.
STEP 2: Declare the class as Prime with data members,
Member functions.
STEP 3: Consider the argument constructor Prime() with integer
Argument.
STEP 4: To cal the function calculate() and do the following steps.
STEP 5: For i=2 to a/2 do
STEP 6: Check if a%i==0 then set k=0 and break.
STEP 7: Else set k value as 1.
STEP 8: Increment the value i as 1.
STEP 9: Check whether the k value is 1 or 0.
STEP 10:If it is 1 then display the value is a prime number.
STEP 11:Else display the value is not prime.
STEP 12:Stop the program.
C++ PROGRAM:
01 | #include<iostream.h> |
02 | #include<conio.h> |
03 | class prime |
04 | { |
05 | int a,k,i; |
06 | public: |
07 | prime(int x) |
08 | { |
09 | a=x; |
10 | } |
11 | void calculate() |
12 | { |
13 | k= 1 ; |
14 | { |
15 | for(i= 2 ;i<=a/ 2 ;i++) |
16 |
17 | if(a%i== 0 ) |
18 | { |
19 | k= 0 ; |
20 | break; |
21 | } |
22 | else |
23 | { |
24 | k= 1 ; |
25 | } |
26 | } |
27 | } |
28 |
29 | void show () |
30 | { |
31 | if(k== 1 ) |
32 | cout<< “\n\tA is prime Number. "; |
33 | else |
34 | cout<< "\n\tA is Not prime." ; |
35 | } |
36 | }; |
37 |
38 | void main() |
39 | { |
40 | clrscr(); |
41 | int a; |
42 | cout<< "\n\tEnter the Number:" ; |
43 | cin>>a; |
44 | prime obj(a); |
45 | obj.calculate(); |
46 | obj. show (); |
47 | getch(); |
48 | } |
Enter the number: 7
Given number is Prime Number
To calculate factorial of a given number using copy constructor.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as Copy with data members and member functions.
STEP 3: The constructor Copy() with argument to assign the value.
STEP 4: To cal the function calculate() do the following steps.
STEP 5: For i=1 to var do
STEP 6: Calculate fact*i to assign to fact.
STEP 7: Increment the value as 1.
STEP 8: Return the value fact.
STEP 9: Print the result.
STEP 10: Stop the program.
PROGRAM:
01 | #include<iostream.h> |
02 | #include<conio.h> |
03 | class copy |
04 | { |
05 | int var,fact; |
06 | public: |
07 |
08 | copy(int temp) |
09 | { |
10 | var = temp; |
11 | } |
12 |
13 | double calculate() |
14 | { |
15 | fact= 1 ; |
16 | for(int i= 1 ;i<=var;i++) |
17 | { |
18 | fact = fact * i; |
19 | } |
20 | return fact; |
21 | } |
22 | }; |
23 | void main() |
24 | { |
25 | clrscr(); |
26 | int n; |
27 | cout<< "\n\tEnter the Number : " ; |
28 | cin>>n; |
29 | copy obj(n); |
30 | copy cpy=obj; |
31 | cout<< "\n\t" <<n<< " Factorial is:" <<obj.calculate(); |
32 | cout<< "\n\t" <<n<< " Factorial is:" <<cpy.calculate(); |
33 | getch(); |
34 | } |
Enter the Number: 5
Factorial is: 120
Factorial is: 120
Perform exception handling for Divide by zero Exception
perform exception handling for Divide by zero Exception.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables a,b,c.
Step 3: Read the values a,b,c,.
Step 4: Inside the try block check the condition.
a. if(a-b!=0) then calculate the value of d and display.
b. otherwise throw the exception.
Step 5: Catch the exception and display the appropriate message.
Step 6: Stop the program.
PROGRAM:
Output:
Enter the value for a: 20
Enter the value for b: 20
Enter the value for c: 40
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables a,b,c.
Step 3: Read the values a,b,c,.
Step 4: Inside the try block check the condition.
a. if(a-b!=0) then calculate the value of d and display.
b. otherwise throw the exception.
Step 5: Catch the exception and display the appropriate message.
Step 6: Stop the program.
PROGRAM:
01 | #include<iostream.h> |
02 | #include<conio.h> |
03 | void main() |
04 | { |
05 | int a,b,c; |
06 | float d; |
07 | clrscr(); |
08 | cout<< "Enter the value of a:" ; |
09 | cin>>a; |
10 | cout<< "Enter the value of b:" ; |
11 | cin>>b; |
12 | cout<< "Enter the value of c:" ; |
13 | cin>>c; |
14 |
15 | try |
16 | { |
17 | if((a-b)!= 0 ) |
18 | { |
19 | d=c/(a-b); |
20 | cout<< "Result is:" <<d; |
21 | } |
22 | else |
23 | { |
24 | throw(a-b); |
25 | } |
26 | } |
27 |
28 | catch(int i) |
29 | { |
30 | cout<< "Answer is infinite because a-b is:" <<i; |
31 | } |
32 |
33 | getch(); |
34 | } |
Enter the value for a: 20
Enter the value for b: 20
Enter the value for c: 40
To perform exception handling with multiple catch.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare and define the function test().
Step 3: Within the try block check whether the value is greater than zero or not.
a. if the value greater than zero throw the value and catch the corresponding exception.
b. Otherwise throw the character and catch the corresponding exception.
Step 4: Read the integer and character values for the function test().
Step 5: Stop the program.
PROGRAM:
Output:
Testing multiple catches
Catch a integer and that integer is: 10
Catch a character and that character is: x
Step 1: Start the program.
Step 2: Declare and define the function test().
Step 3: Within the try block check whether the value is greater than zero or not.
a. if the value greater than zero throw the value and catch the corresponding exception.
b. Otherwise throw the character and catch the corresponding exception.
Step 4: Read the integer and character values for the function test().
Step 5: Stop the program.
PROGRAM:
01 | #include<iostream.h> |
02 | #include<conio.h> |
03 | void test(int x) |
04 | { |
05 | try |
06 | { |
07 | if(x> 0 ) |
08 | throw x; |
09 | else |
10 | throw 'x' ; |
11 | } |
12 |
13 | catch(int x) |
14 | { |
15 | cout<< "Catch a integer and that integer is:" <<x; |
16 | } |
17 |
18 | catch(char x) |
19 | { |
20 | cout<< "Catch a character and that character is:" <<x; |
21 | } |
22 | } |
23 |
24 | void main() |
25 | { |
26 | clrscr(); |
27 | cout<< "Testing multiple catches\n:" ; |
28 | test( 10 ); |
29 | test( 0 ); |
30 | getch(); |
31 | } |
Testing multiple catches
Catch a integer and that integer is: 10
Catch a character and that character is: x
To find the mean value of a given number using friend function.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as Base with data members and member functions.
STEP 3: The function get() is used to read the 2 inputs from the user.
STEP 4: Declare the friend function mean(base ob) inside the class.
STEP 5: Outside the class to define the friend function and do the following.
STEP 6: Return the mean value (ob.val1+ob.val2)/2 as a float.
STEP 7: Stop the program.
PROGRAM:
Output:
Enter two values: 10, 20
Mean Value is: 15
STEP 1: Start the program.
STEP 2: Declare the class name as Base with data members and member functions.
STEP 3: The function get() is used to read the 2 inputs from the user.
STEP 4: Declare the friend function mean(base ob) inside the class.
STEP 5: Outside the class to define the friend function and do the following.
STEP 6: Return the mean value (ob.val1+ob.val2)/2 as a float.
STEP 7: Stop the program.
PROGRAM:
01 | #include<iostream.h> |
02 | #include<conio.h> |
03 | class base |
04 | { |
05 | int val 1 ,val 2 ; |
06 | public: |
07 | void get() |
08 | { |
09 | cout<< "Enter two values:" ; |
10 | cin>>val 1 >>val 2 ; |
11 | } |
12 | friend float mean(base ob); |
13 | }; |
14 | float mean(base ob) |
15 | { |
16 | return float(ob.val 1 +ob.val 2 )/ 2 ; |
17 | } |
18 | void main() |
19 | { |
20 | clrscr(); |
21 | base obj; |
22 | obj.get(); |
23 | cout<< "\n Mean value is : " <<mean(obj); |
24 | getch(); |
25 | } |
Output:
Enter two values: 10, 20
Mean Value is: 15
To calculate the area of circle, rectangle and triangle using function overloading. July 8, 2011
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as fn with data members and member functions.
STEP 3: Read the choice from the user.
STEP 4: Choice=1 then go to the step 5.
STEP 5: The function area() to find area of circle with one integer argument.
STEP 6: Choice=2 then go to the step 7.
STEP 7: The function area() to find area of rectangle with two integer argument.
STEP 8: Choice=3 then go to the step 9.
STEP 9: The function area() to find area of triangle with three arguments, two as Integer and one as float.
STEP 10: Choice=4 then stop the program.
PROGRAM:
Output:
Function Overloading
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 2
Enter the Sides of the Rectangle: 5 5
Area of Rectangle is: 25
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 4
STEP 1: Start the program.
STEP 2: Declare the class name as fn with data members and member functions.
STEP 3: Read the choice from the user.
STEP 4: Choice=1 then go to the step 5.
STEP 5: The function area() to find area of circle with one integer argument.
STEP 6: Choice=2 then go to the step 7.
STEP 7: The function area() to find area of rectangle with two integer argument.
STEP 8: Choice=3 then go to the step 9.
STEP 9: The function area() to find area of triangle with three arguments, two as Integer and one as float.
STEP 10: Choice=4 then stop the program.
PROGRAM:
01 | #include<iostream.h> |
02 | #include<stdlib.h> |
03 | #include<conio.h> |
04 | #def ine pi 3.14 |
05 | class fn |
06 | { |
07 | public: |
08 | void area(int); // circle |
09 | void area(int,int); //rectangle |
10 | void area(float ,int,int); //triangle |
11 | }; |
12 |
13 | void fn::area(int a) |
14 | { |
15 | cout<< "Area of Circle:" <<pi*a*a; |
16 | } |
17 | void fn::area(int a,int b) |
18 | { |
19 | cout<< "Area of rectangle:" <<a*b; |
20 | } |
21 | void fn::area(float t,int a,int b) |
22 | { |
23 | cout<< "Area of triangle:" <<t*a*b; |
24 | } |
25 |
26 | void main() |
27 | { |
28 | int ch; |
29 | int a,b,r; |
30 | clrscr(); |
31 | fn obj; |
32 | cout<< "\n\t\tFunction Overloading" ; |
33 | cout<<"\n 1 .Area of Circle\n 2 .Area of Rectangle\n 3 .Area of Triangle\n 4 .Exit\n:”; |
34 | cout<<”Enter your Choice:"; |
35 | cin>>ch; |
36 |
37 | switch(ch) |
38 | { |
39 | case 1: |
40 | cout<< "Enter Radious of the Circle:" ; |
41 | cin>>r; |
42 | obj.area(r); |
43 | break; |
44 | case 2: |
45 | cout<< "Enter Sides of the Rectangle:" ; |
46 | cin>>a>>b; |
47 | obj.area(a,b); |
48 | break; |
49 | case 3: |
50 | cout<< "Enter Sides of the Triangle:" ; |
51 | cin>>a>>b; |
52 | obj.area( 0.5 ,a,b); |
53 | break; |
54 | case 4: |
55 | exit( 0 ); |
56 | } |
57 | getch(); |
58 | } | | | | | | |
Output:
Function Overloading
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 2
Enter the Sides of the Rectangle: 5 5
Area of Rectangle is: 25
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 4
Langganan:
Postingan (Atom)