What's new

C++ Function (Hiding) ?

Welcome Guest

Forum Veteran
Joined
Nov 29, 2015
Posts
1,000
Reaction
146
Points
589
Age
27
Hello po sa mga master dyan sa c++. Ask lng po ako about function that returns a value. Paano ko po ma hide ung function. Ang sabi ng prof ko ay dapat ma e hide ko.
Dapat ang laman ng main function ay 2lines lng, included na ang return 0;. Ito po ang codes ko. Please help me...

My Code:

#include <iostream>
using namespace std;

double addition (int, int);
double subtraction (int, int);
double multiplication (int, int);
double division (int, int);

int main()
{
char ans;
double num1, num2;

cout << "A.Addition \nB.Subtraction \nC.Multiplication \nD.Division\n";
cout << "\nWhat do you want to do: ";
cin >> ans;

switch (ans)
{
case 'a': cout << "You've chosen Addition.\n";
cout << "Enter the 1st number: ";
cin >> num1;
cout << "Enter the 2nd number: ";
cin >> num2;
cout << "The sum of " << num1 << " & " << num2 << " is " << addition(num1,num2);
break;

case 'b': cout << "You've chosen Subtraction.\n";
cout << "Enter the 1st number: ";
cin >> num1;
cout << "Enter the 2nd number: ";
cin >> num2;
cout << "The difference of " << num1 << " & " << num2 << " is " << subtraction(num1,num2);
break;

case 'c': cout << "You've chosen Subtraction.\n";
cout << "Enter the 1st number: ";
cin >> num1;
cout << "Enter the 2nd number: ";
cin >> num2;
cout << "The product of " << num1 << " & " << num2 << " is " << multiplication(num1,num2);
break;

case 'd': cout << "You've chosen Division.\n";
cout << "Enter the 1st number: ";
cin >> num1;
cout << "Enter the 2nd number: ";
cin >> num2;
cout << "The quotient of " << num1 << " & " << num2 << " is " << division(num1,num2);
break;
}
}

double addition (int num1, int num2)
{
int sum = num1+num2;
return sum;
}

double subtraction (int num1, int num2)
{
int difference = num1-num2;
return difference;
}
double multiplication (int num1, int num2)
{
int product = num1*num2;
return product;
}
double division (int num1, int num2)
{
int quotient = num1/num2;
return quotient;
}


My prof's example code of hiding function:
#include <iostream>
using namespace std;

void add(int,int);
void get2nums();

int main()
{
get2nums();
return 0;
}

void add(int i, int x)
{
int y = i+x;
cout << y;
}

void get2nums()
{
int num1,num2;
cout << "Enter 2 numbers: ";
cin >> num1 >> num2;
add(num1,num2);
}
 
Pwede yan using Function calling other function.
C++:
#include <iostream>
using namespace std;
void addition(){
   
}
void subtraction(){
   
}
void process(){
    int choice = 1;
    switch(choice){
        case 1:
            addition();
            break;
        case 2:
            subtraction();
            break;
        // and so on
    }
}
int main() {
    process();
    return 0;
}
 
Last edited:
Pwede yan using Function calling other function.
C++:
#include <iostream>
using namespace std;
void addition(){
  
}
void subtraction(){
  
}
void process(){
    int choice = 1;
    switch(choice){
        case 1:
            addition();
            break;
        case 2:
            subtraction();
            break;
        // and so on
    }
}
int main() {
    process();
    return 0;
}
Kailangan void po ba tlga gamitin?
 
anong year kana boss? buti natututukan kayo ng prof nyo kahit online class samin kasi ang tamad eh need sariling sikap.
 
your using C++ to code a C language program, that's a procedural style yet you're using an object oriented programming language. tsaka bakit hiding ang tawag dun, parang may mali sa approach ng prof mo sir. basahin mo na lang ung libro nung gumawa mismo ng C at C++ para ma guide ka ng tama.
 
your using C++ to code a C language program, that's a procedural style yet you're using an object oriented programming language. tsaka bakit hiding ang tawag dun, parang may mali sa approach ng prof mo sir. basahin mo na lang ung libro nung gumawa mismo ng C at C++ para ma guide ka ng tama.
Hiding data daw po sabi ng prof ko, oop also known as encapsulation daw po.
 
aysus, hinde ganun ung encapsulation, ni hinde nga nakalagay sa class yung mga methods ni prof. mo, naku kung ako sayo self learn ka na lang and get your information from the experts sa field na yan, download ka libro nila, nakalagay yan dun. tsaka always code, best teacher yan, isa sa pinaka challenging na PL ang C++, di mo rin masisi yan prof mo baka ganyan din turo ng teacher nya, e anjan na yan hehe, gawin mo na lang. mas okay talaga kung magaling mag self learn lalo na sa programming.
Hiding data daw po sabi ng prof ko, oop also known as encapsulation daw po.
 
aysus, hinde ganun ung encapsulation, ni hinde nga nakalagay sa class yung mga methods ni prof. mo, naku kung ako sayo self learn ka na lang and get your information from the experts sa field na yan, download ka libro nila, nakalagay yan dun. tsaka always code, best teacher yan, isa sa pinaka challenging na PL ang C++, di mo rin masisi yan prof mo baka ganyan din turo ng teacher nya, e anjan na yan hehe, gawin mo na lang. mas okay talaga kung magaling mag self learn lalo na sa programming.
Tama po kayo, java po kc major nya tapos na assign siya sa c++ bigla kc nag resign ung isang prof namin kaya siguro nag rereview lng sya or nanibago sya. Salamat din po sa tips nyo about self learning. Saan po ba ako pwede mag self study about c++? Kasi wala akong makitang site na nag te train ng c++ mostly may bayad. Ang problema ko po minsan sa self study ay wala akong makuhang machine problems pra pagpraktisan, makahanap man ako at pagktpos ko e code ay nd ko naman alm kung tama ba ang output ko.
 

Similar threads

Back
Top