Why does vscode tell constructor defined outside class is inaccesible? - class

I have defined a constructor and then tried initializing an object but vscode tells me that the constructor is inaccessible. I don't understand what the problem is
this is my code
using namespace std;
#include<iostream>
class player{
// attributes
int xp{0};
string name;
int health{0};
float avg_score{0};
int tot{0};
int c{0};
// methods
void add_score(int score){
tot += score;
c++;
};
void display_avg_score(){
avg_score = tot/c;
cout << avg_score << endl;
};
void player_is_perfect(){
if((xp > 5) && (avg_score > 23)){
cout << "Perfect"<< endl;
}
};
// defining a constructor
player(int exp,float avg);
};
player::player(int exp,float avg){
xp = exp;
avg_score = avg;
};
int main(){
player frank{23,45.6};
};

As Raymond specified, classes have default access specifier "private". Anything you want to access outside the class should be preceded by the "public" statement like so
class player{
// attributes
int xp{0};
string name;
int health{0};
float avg_score{0};
int tot{0};
int c{0};
public:
// methods
void add_score(int score){
tot += score;
c++;
}
// rest of class
};

Related

C++ Parent Class with Abstracted Function that Uses Functions in a Child Class

I am trying to create a C++ parent class that has two functions, f1 and f2, to be implemented in the child class. This parent class has a function, abstractedFunction that abstracts how f1 and f2 should be used together. Both f1 and f2 are implemented in the child class as shown in the code below.
#include <iostream>
class Parent
{
public:
int f1(); // To be implemented in the derived class
void f2(int i); // To be implemented in the derived class
void abstractedFunction() { // Abstracted in the parant class
auto r = f1();
f2(r);
}
};
class Child : public Parent
{
public:
int f1() {
std::cout << "f1 is implemented in the child class\n";
return 1;
}
void f2(int i) {
std::cout << "f2 is implemented in the child class\n";
std::cout << "Return value for f1 = " << i << "\n";
}
};
int main() {
Child ch;
ch.abstractedFunction();
return 0;
}
Is such a concept implementable in C++?
Yes, You can do something like this. You need to make the functions defined in base class as pure virtual : Follow this link to know more about them and then you can create an object of derived class and assign it to the base pointer to make a required function call
#include <iostream>
using namespace std;
class Parent
{
public:
virtual int f1()=0; // To be implemented in the derived class
virtual void f2(int i)=0; // To be implemented in the derived class
void abstractedFunction() { // Abstracted in the parant class
auto r = f1();
f2(r);
}
};
class Child : public Parent
{
public:
int f1() {
std::cout << "f1 is implemented in the child class\n";
return 1;
}
void f2(int i) {
std::cout << "f2 is implemented in the child class\n";
std::cout << "Return value for f1 = " << i << "\n";
}
};
int main() {
Parent *ptr;
Child c;
ptr=&c;
ptr->abstractedFunction();
return 0;
}

Classes in class in c++

I have this block of code below, and I cant find out what that class ContractB : public: ContractA means?
#include
using namespace std;
class ContractA
{
unsigned int ether = 0;
public:
ContractA(unsigned int e) :ether(e) {}
auto sendEther() { return ether; }
};
class ContractB : public ContractA
{
unsigned int wei = 1;
public:
ContractB(unsigned int w) :wei(w) {}
auto sendWei() { return wei; }
};
int main()
{
ContractB b(0);
cout << b.sendEther() << " " << b.sendWei();
return 0;
}
It represents inheritance. 'public' is the access specifier that limits the most accessible level for the members inherited from the base class (ContractA).
You can read more about it here.

Point class. distance formula. logic issue when plugging coordinate from the constructor and getting wrong result

#include <iostream>
#include <cmath>
using namespace std;
class point{ // define point class
private:
float x=0;
float y=0;
public:
point();// default constructor
point(float, float);// constructor
void setX(float);
void setY(float);
double getX()const;
double getY()const;
};
//implement all the member function
point::point(){ }
point::point(float i, float k){
x=i;
y=k;
}
void point::setX(float xc){
x=xc;
}
void point::setY(float yc){
y=yc;
}
double point::getY()const{
return y;
}
double point::getX()const{
return x;
}
double operator + (const point&lhs, const point &rhs) // free function.
{
double dx=lhs.getX()-rhs.getX();
double dy=lhs.getX()-rhs.getY();
return sqrt(dx*dx+dy*dy);
}
int main(){
point p1(2, -1);
point p2(1, 5);
int dist=0;
dist = p1 + p2;
cout << "The distance between p1 " << "and p2" << " is " << dist << endl;
return 0;
}
This should be 5 but I got 3. I do not understand why?
replace
double dy=lhs.getX()-rhs.getY();
by
double dy=lhs.getY()-rhs.getY()

Want to reset integers using virtual class function

The function I want works within the class butt won't apply to main. Must maintain the initial (Entity *entity = new Nummchange(flarb);)
#include <iostream>
using namespace std;
class Entity
{
public:
Entity(){}
~Entity(){}
virtual int reset(int NUMM) = NULL;
protected:
private:
};
class Nummchange : public Entity
{
public:
Nummchange(int NUMM);
~Nummchange();
int reset(int NUMM);
protected:
private:
int numm;
};
Nummchange::Nummchange(int NUMM)
{
}
Nummchange::~Nummchange()
{
}
int Nummchange::reset(int NUMM)
{
numm = 50;
NUMM = numm;
std::cout << "\nnumm+++++++"<< numm << "\n" << std::endl;
return numm;
}
int main()
{
int flarb = 50;
Entity *entity = new Nummchange(flarb);
while (flarb >= 0)
{
flarb--;
cout << flarb;
if(flarb == 0)
{
entity->reset(flarb);
std::cout << "flarb+++++++"<< flarb << "\n" << std::endl;
}
}
system("pause");
return 0;
}
Success is if the while loop continues perpetually.
int reset(int NUMM); method should take reference of integer type as below.
int reset(int &NUMM);
Make sure that you change the parameter to be reference of integer in all the three places of the method use in the program.

member function as callback

I would like to pass a member function of an instantiated object to another function. Example code is below. I am open for any strategy that works, including calling functional() from another function inside memberfuncpointertestclass using something like lambda or std::bind. Please note that I did not understand most of the threads I found with google about lambda or std::bind, so please, if possible, keep it simple. Also note that my cluster does not have C++ 11 and I would like to keep functional() as simple as it is. Thank you!
int functional( int (*testmem)(int arg) )
{
int a = 4;
int b = testmem(a);
return b;
}
class memberfuncpointertestclass
{
public:
int parm;
int member( int arg )
{
return(arg + parm);
}
};
void funcpointertest()
{
memberfuncpointertestclass a;
a.parm = 3;
int (*testf)(int) = &a.member;
std::cout << functional(testf);
}
int main()
{
funcpointertest();
return 0;
}
You cannot invoke a method on an object without an instance to refer to. So, you need to pass in both the instance as well as the method you want to invoke.
Try changing functional to:
template <typename T, typename M>
int functional(T *obj, M method)
{
int a = 4;
int b = (obj->*(method))(a);
return b;
}
And your funcpointertest to:
void funcpointertest()
{
memberfuncpointertestclass a;
a.parm = 3;
std::cout << functional(&a, &memberfuncpointertestclass::member);
}
This is a job for std::function, a polymorphic function wrapper. Pass to functional(...) such a function object:
#include <functional>
typedef std::tr1::function<int(int)> CallbackFunction;
int functional(CallbackFunction testmem)
{
int a = 4;
int b = testmem(a);
return b;
}
then use std::bind to create a function object of the same type that wraps memberfuncpointertestclass::method() of instance a:
void funcpointertest()
{
memberfuncpointertestclass a;
a.parm = 3;
CallbackFunction testf = std::bind(&memberfuncpointertestclass::member, &a, std::placeholders::_1);
std::cout << functional(testf);
}
Check this item for more details.