How I can use friend functions? - c++

I started to learn some oop and I have a question, why I can't put the function in first class, I know there is a way to write the friend method down under second class. If I put it in the first class where it belongs my compiler shows the error C2027: "Error C2027 use of undefined type 'Calculator'"
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <ctime>
using namespace std;
class Calculator;
class PlacadeBaza {
char denumire_procesor[100];
public:
char* getDenumire() {
return denumire_procesor;
}
void set(Calculator a, PlacadeBaza b, int memorie, char denumire[100]) { //C2027
a.memorie_RAM = memorie;
strcpy(b.denumire_procesor, denumire); //C2027
}
};
class Calculator {
int memorie_RAM;
public:
int getMemorie_RAM() {
return memorie_RAM;
}
friend void PlacadeBaza::set(Calculator a, PlacadeBaza b, int memorie, char denumire[100]);
};
int main() {
Calculator a;
PlacadeBaza b;
int memorie;
char denumire[100];
cin >> memorie >> denumire;
b.set(a, b, memorie, denumire);
cout << a.getMemorie_RAM();
cout << b.getDenumire();
}

Setting aside other issues with the code, if you wish PlacadeBaza to be able to access private members of Calculator you need to declare that PlacadeBaza is a friend class of Calculator.
#include <iostream>
#include <ctime>
using namespace std;
class PlacadeBaza;
class Calculator {
int memorie_RAM;
public:
int getMemorie_RAM() {
return memorie_RAM;
}
friend class PlacadeBaza;
};
class PlacadeBaza {
char denumire_procesor[100];
public:
char* getDenumire() {
return denumire_procesor;
}
void set(Calculator a, PlacadeBaza b, int memorie, char denumire[100]) { //C2027
a.memorie_RAM = memorie;
strcpy(b.denumire_procesor, denumire); //C2027
}
};
int main() {
Calculator a;
PlacadeBaza b;
int memorie;
char denumire[100];
cin >> memorie >> denumire;
b.set(a, b, memorie, denumire);
cout << a.getMemorie_RAM();
cout << b.getDenumire();
}

Related

What syntax should i use for member initializers?

What syntax should i use for member initializers in c++?
I know how to use it for Attribute but not for functions.
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass(int a, int b,int c):
regVar(a), constVar(b),function(c)
{}
public:
int regVar;
const int constVar;
void function(int a){
cout << a;
}
};
int main(){
MyClass myclass(10,20);
cout << myclass.regVar;
}
Thanks in advance
I tried function(c), c.function etc.

Basic Class Inheritance in C++

I am typing the following code and I am getting the following error at line-1
[Error] no matching function for call to 'int_adder::add()
#include <iostream>
using namespace std;
class adder{
public:
void add(){ cout <<"adder::add() "; }
};
class int_adder : public adder{
public:
int add(int a, int b){
return (a + b);
}
};
int main(){
int_adder ia;
ia.add(); //LINE-1
cout << ia.add(10, 20); //LINE-2
return 0;
}
As pointed by others in the comment, I have corrected it:-
#include <iostream>
using namespace std;
class adder{
public:
void add(){ cout <<"adder::add() "; }
};
class int_adder : public adder{
public:
int add(int a, int b){
return (a + b);
}
};
int main(){
int_adder ia;
ia.adder::add(); //LINE-1
cout << ia.add(10, 20); //LINE-2
return 0;
}
The statement adder::add() will overide the function add() present in int_adder.
Can't fing exact dupe, but you can make overloads from base class visible by using using directive, example:
#include <iostream>
using namespace std;
class adder{
public:
void add(){ cout <<"adder::add() "; }
};
class int_adder : public adder{
public:
using adder::add; // expose base class overload as our own
int add(int a, int b){
return (a + b);
}
};
int main(){
int_adder ia;
ia.add(); //LINE-1
ia.adder::add(); // explicit name also works
cout << ia.add(10, 20); //LINE-2
return 0;
}
As the other answer mentions using base class name scope also works. It all depends on your needs and class design.
Basically defining an overload in a derived class prevents implicit method look up from matching base class overloads, so you have to be explicit about it in one way or another.
You have totally two different versions of add in the first place. You are not overriding, you are overloading. You are just providing a new add function that has nothing to do with the other add function of the parent.
So first of all, do you want to override or overload?
Overriding the parent add would like the following:
#include <iostream>
using namespace std;
class adder{
public:
void add(){ cout <<"adder::add() "; }
};
class int_adder : public adder{
public:
int add() override {cout <<"int_adder ::add() ";};
int add(int a, int b){
return (a + b);
}
};
int main(){
int_adder ia;
ia.adder::add(); //LINE-1 <- would work displays adder::add()'s message
cout << ia.add(10, 20); //LINE-2
adder ia = int_adder{}; // now this is interesting
ia.add(); // would work displays adder::add()'s message - cause you did not ask for virtuality
return 0;
}

Program in c++ to use 2 classes and find maximum of 2 numbers

I want to find the maximum of 2 numbers but instead of the simple method, i need to use 2 classes and friend functions. How to implement it?
I am using the following code but the code isn't working.
#include<iostream>
using namespace std;
class one
{
int a;
public:
friend int cal(one a);
};
class two
{
int b;
public:
friend int cal(one a,two b);
};
cal(int f,int g)
{
int ans=(x.a>y.b)?x.a:y.b;
}
int main()
{
one x;
two y;
cal(10,20);
}
#include<iostream>
using namespace std;
class biggest
{
private:
int a,b;
public:
void input();
void display();
};
void biggest::input()
{
cout<<"Enter 2 nos.:";
cin>>a>>b;
}
void biggest::display()
{
if(a>b)
cout<<"Biggest no.:"<<a;
else
cout<<"Biggest no.:"<<b;
}
int main()
{
biggest b;
b.input();
b.display();
}
Output
Enter 2 nos.:133 21
Sample Output
Biggest no.:133
By setting a function as a "friend", you give it access to private members of a class. Example looked really strange, but I guess this will do it. Both classes here give private members access to "cal" function.
#include<iostream>
using namespace std;
class one;
class two;
class one
{
int a = 10;
public:
friend int cal(one a,two b);
};
class two
{
int b = 20;
public:
friend int cal(one a,two b);
};
int cal(one x,two y)
{
return (x.a>y.b)?x.a:y.b;
}
int main()
{
one x;
two y;
cout << cal(x,y);
}

feets to inches with class to class type conversion

i was making a c++ program with class to class type conversion and i wrote this code... the variable i is not being affected by type conversion
i tried taking it outside but i cant make it work...please help
#include<conio.h>
#include<iostream>
using namespace std;
class feet
{
int f;
public:
void inputfeet()
{
cin>>f;
}
get()
{
return (f);
}
};
class inches
{
int i;
public:
void inputinches()
{
cin>>i;
}
void operator - (feet x){
i=i+((x.get())*12);
}
void show(){
cout<<"inches"<<i;
}
};
int main()
{
feet foot;
inches inch;
foot.inputfeet();
inch.inputinches();
-inch;
inch.show();
return 0;
}

Passing member function as parameter to another function in c++

I have the following problem.
I have a base abstract class with a pure virtual method, and I want to pass it as an argument to another member function(so not a normal function). Yet I have an error when trying to call the method with specified function. Code speaks better than words so bellow I have posted the code that generates the problem
class BaseClass
{
public:
BaseClass();
int add(int, int);
virtual void op(void (*f)(int, int), string s, int a, int b) = 0;
~BaseClass();
};
#include "BaseClass.h"
class ClasaDerviata:public BaseClass
{
public:
ClasaDerviata();
void testNumere(int a, int b);
void op(void(*f)(int, int), string s, int a, int b);
~ClasaDerviata();
};
#include "BaseClass.h"
BaseClass::BaseClass()
{
}
int BaseClass::add(int a, int b)
{
return a + b;
}
BaseClass::~BaseClass()
{
}
#include "ClasaDerviata.h"
#include <iostream>
using namespace std;
ClasaDerviata::ClasaDerviata()
{
}
void ClasaDerviata::testNumere(int a, int b)
{
cout << a + b << "\n";
cout << " suma " << add(a,b) << "\n";
}
void ClasaDerviata :: op (void (*f)(int, int), string s, int a, int b)
{
f(a, b);
cout << s << "\n";
}
ClasaDerviata::~ClasaDerviata()
{
}
#include "ClasaDerviata.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ClasaDerviata *a;
a = new ClasaDerviata();
int x, y;
cin >> x >> y;
a->op(&ClasaDerviata::testNumere, "test metoda", x, y);
system("pause");
return 0;
}
Thank you for your time!
void ClasaDerviata::testNumere(int a, int b); is not of type void (*)(int, int) but void (ClasaDerviata::*)(int, int)
You may add static to testNumere and add to fix your problem or change signature of your function (and change internal code too).
Remember when you make a call to a member function a hidden parameter 'this' is passed. So your ClasaDerviata::testNumere(int a, int b); function actually takes three parameters.
I would suggest to read Joseph Garvin comment in
How can I pass a class member function as a callback?
he has explained it very well.