Unknown Override specifier when accessing methods/object across classes - c++

What I want to do (c++ problem):
Enter main. Call Class A (and pass a value). Inside class A, I call Class B (and pass a value). Do some stuff in class B. Return value back to Class A. Do some more stuff in A. Return back to main function.
I get the error that obj3 is an unknown override specifier. I tried to create a simple program to showcase my problem;
#include <math.h>
#include <string>
#include <stdio.h>
#include <iomanip>
#include <iostream>
using namespace std;
class A
{
B obj3;
public:
int add3(int num)
{
int x = num + 1;
int y = obj3.add2(x);
return y;
}
};
class B
{
public:
int add2(int num2)
{
int y = num2 + 2;
return y;
}
};
int main()
{
int g;
A obj1;
cout << "enter a number: " << endl;
cin >> g;
int r = obj1.add3(g);
cout << r;
system("pause");
return 0;
}

Related

Return pointer to array virtual template function

I would like to return an array to a pointer, in a virtual function that is a member of a derived class of a template class. In details, my classes definition is:
Sampler.h
#ifndef SAMPLER_H
#define SAMPLER_H
template <class T>
class Sampler
{
public:
virtual T getnumber()=0;
virtual T* simulation(int n)=0;
};
class UniformSampler:public Sampler<double>
{
public:
virtual double getnumber();
virtual double* simulation(int n);
UniformSampler(double a=0.0, double b=1.0);
private:
double low_bound;
double up_bound;
};
#endif
The class Sampler is a template class in order to be able to derive an other sampler with vectors later. The implementation is:
Sampler.cpp
#include "Sampler.h"
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
//Uniform
UniformSampler::UniformSampler(double a, double b)
{
low_bound=a;
up_bound=b;
}
double UniformSampler::getnumber()
{
int myrand=rand();
while((myrand==0)||(myrand==RAND_MAX)){myrand = rand(); } //We want a number in (0, RAND_MAX).
double myuni = myrand/static_cast<double>(RAND_MAX); //Create a number in (0,1).
return low_bound + myuni*(up_bound-low_bound);
}
double* UniformSampler::simulation(int n){
double simulations[n];
for(int i=0; i<n; i++){
simulations[i] = this->getnumber();
}
return simulations;
}
My problem is that, when I try to call this program in the main(), it looks like the assignment of the pointer doesn't work. Here is my main.cpp:
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <time.h>
using namespace std;
#include "Sampler.h"
int main(){
srand(time(0));
int n=10;
double *unif = new double[n];
UniformSampler uni;
unif = uni.simulation(n);
for ( int i = 0; i < n; i++ ) {
cout << "*(p + " << i << ") : ";
cout << *(unif + i) << endl;
}
delete[] unif;
return 0;
}
When I run it, it doesn't print any of the elements that unif points to. I don't understand what is wrong there.
UniformSampler::simulation is twice wrong:
double simulations[n]; uses VLA extension, so not C++ standard compliant.
you return pointer on local variable, so dangling pointer.
Solution: use std::vector instead.
#include <vector>
template <class T>
class Sampler
{
public:
virtual ~Sampler() = default;
virtual T getnumber() = 0;
virtual std::vector<T> simulation(int n) = 0;
};
class UniformSampler:public Sampler<double>
{
public:
explicit UniformSampler(double a=0.0, double b=1.0);
double getnumber() overrid;
std::vector<double> simulation(int n) override
{
std::vector<double> res(n);
for (auto& val : res){
res = getnumber();
}
return res;
}
private:
double low_bound;
double up_bound;
};
int main(){
srand(time(0));
constexpr int n = 10;
UniformSampler uni;
auto unif = uni.simulation(n);
for (int i = 0; i < n; i++ ) {
std::cout << "p[" << i << "]: " << unif[i] << endl;
}
}

How use value of variable in another class

Imagine I have a class and it has a private value for example this value name is a, then I set it's value to 10.
How I can access to this variable with its value (10) in another class?
(I do not want to use friend function and friend class)
a.h
#include <iostream>
using namespace std;
static int s=0;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class a
{
private:
public:
void sets(int );
};
a.cpp
#include "a.h"
void a::sets(int y){
cin >> y;
s=y;
}
main.cpp
#include"a.h"
int main()
{
int i=0;
int q;
a a1;
a1.sets(q);
cout << s+1 << endl;
for (i=1; i<5; i++){
if (s == i) cout << "ok";
}
}
If you do not want to use friendship, add a public interface to this variable (setter and/or getter class members).
Either make the variable public or add a function to get the value that returns it, like int getA() const { return a; } .
Use a private member s in your class a instead of a global variable, and access it via public Getter function GetS
Example:
#include <iostream>
using namespace std;
class a
{
private:
int s;
public:
void SetS() { cin >> s; }
int GetS() const { return s; }
};
int main()
{
a a1;
a1.SetS();
int s1=a1.GetS();
cout << s1+1 << endl;
for (int i=1; i<5; i++){
if (s1 == i) cout << "ok";
}
}

Getting 2 ERROR Messeges Eclipse compiler: non static member and error 2

I am new to C++, I have been doing tasks for training. This task was to make a calculation while using class and accessing private integer.
here is my full code.
#include <iostream>
using namespace std;
class Calculatour{
public:
int SumNum(int a, int b){
cin >> a;
cin >> b;
x = a+b;
return x;
}
private:
int x;
};
int main() {
Calculatour ADD;
cout << ADD.SumNum;
return 0;
}
I have been getting an error on this line:
cout << ADD.SumNum;
Where it says
reference to non-static member function must be called ADD calculatour using classes.cpp /ADD calculatour using classes/src line 37 C/C++ Problem.
Also I have been getting this error too:
make: *** [src/ADD calculatour using classes.o] Error 1 ADD calculatour using classes C/C++ Problem
Please consider that i am new to the language. So if you can provide a solution and an explanation this would be really helpful.
Thanks
When invoking a method (or calling a function) with parameters one must supply the parameters even if they are not used. Since in this method you have no intention of using the parameters for anything and have no class hierarchy that forces you to include these parameters, you may as well discard them.
#include <iostream>
using namespace std;
class Calculatour
{
public:
int SumNum()
{
int a;
int b;
cin >> a;
cin >> b;
x = a + b;
return x;
}
private:
int x;
};
int main()
{
Calculatour ADD;
cout << ADD.SumNum();
return 0;
}
The more ideologically correct solution (the Calculator class should do Calculator things, not Data In/Out things) would be to read a and b in in main, and then call SumNum.
#include <iostream>
using namespace std;
class Calculatour
{
public:
int SumNum(int a, int b)
{
x = a + b;
return x;
}
int SumNum(int a) // takes advantage of the stored x value
{
x += a;
return x;
}
private:
int x = 0;
};
int main()
{
Calculatour ADD;
int a;
int b;
cin >> a;
cin >> b;
cout << ADD.SumNum(a, b);
cout << ADD.SumNum(a);
return 0;
}
There is no way for the compiler to tell
int SumNum()
{
int a;
int b;
cin >> a;
cin >> b;
x = a + b;
return x;
}
from
int SumNum()
{
int a;
cin >> a;
x += a;
return x;
}
so you cannot take advantage of overloading and having the same method name perform different tasks with different input.
Say you want
double SumNum()
{
double a;
double b;
cin >> a;
cin >> b;
return a + b;
}
to take floating point input. You can't. You'd have to change the method's name or use templates.

C++ inheritance how to get variables values

I'm trying to create a small program for a lessons.
But In My class that herit , I don't have values of the superclass.
, here's the code
Point.h
#ifndef Point_H
#define Point_H
#include <iostream>
class Point{
public:
Point();
void set_values (int a, int b);
void set_values (int a, int b, int c);
void affichervaleurs();
protected:
int x ;
int y ;
int z ;
};
#endif
Point.cpp
#include <iostream>
#include "Point.h"
using namespace std;
Point::Point(){
x=0;
y=0;
z=0;
};
void Point::set_values (int a, int b){
x=a;
y=b;
}
void Point::set_values (int a = 0, int b = 0, int c = 0){
x=a;
y=b;
z=c;
}
void Point::affichervaleurs(){
cout << "X = " << x << endl;
cout << "Y = " << y << endl;
}
Carre.h
#ifndef Carre_H
#define Carre_H
#include "Point.h"
class Carre:public Point{
public:
int Aire (){
};
void affichercar(){
};
};
#endif
Carre.cpp
#include <iostream>
using namespace std;
#include "Point.h"
class Carre:public Point{
public:
//Carre::Carre(int a, int b);
int Aire (){
return (x * y);
}
void affichercar(){
cout << "Coordonnees X:" << x << endl;
}
};
main.cpp
#include <cstdlib>
#include <iostream>
#include "Carre.h"
#include "Point.h"
using namespace std;
int main()
{
Point MonPoint ;
cout << "Default values:" << endl;
MonPoint.affichervaleurs();
MonPoint.set_values(4,6);
cout << "Setting values:" << endl;
MonPoint.affichervaleurs();
Carre MonCarre;
MonCarre.set_values(4,6,0);
MonCarre.set_values(5,8);
MonCarre.affichercar();
cout << MonCarre.Aire() << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
The problem is, when I call this line :
cout << MonCarre.Aire() << endl;
it returns a strange number. I'm sure that the problem is here :
int Aire (){
return (x * y);
};
Like the x and y are not linked with the superclass.
Can someone help me how to access my x and y values from my superclass in my class Carre ?
Other strange thing this line doesn't appear :
MonCarre.affichercar();
Thanks
You define the functions Aire and affichercar in Carre.h, but you need to declare them only if you want to write an implementation in Carre.cpp.
Edit: and your Carre.cpp is also wrong. You just need to rewrite Carre.* files.
Edit2. Let me do some code review and give you a working version of your code - just summarizing what was discussed in comments and my own thoughts.
Point.h
void set_values (int a, int b);
void set_values (int a, int b, int c);
If you want to define the default values of the arguments of the second function, you should do it in its declaration as was suggested in comments to your question. So it will be
void set_values (int a, int b);
void set_values (int a = 0, int b = 0, int c = 0);
But in this case it's not clear what function should be called if you write, for example, set_values(1, 2), therefore your code won't compile because of ambiguity (as also was mentioned in the comments). So you just have to keep only one extended version of this function with the default arguments. The Point.h will be in this case:
#ifndef Point_H
#define Point_H
#include <iostream>
class Point{
public:
Point();
void set_values (int a = 0, int b = 0, int c = 0);
void affichervaleurs();
protected:
int x ;
int y ;
int z ;
};
#endif
The Point.cpp is also changed a little bit:
#include <iostream>
#include "Point.h"
using namespace std;
Point::Point()
// it's better to initialize these variables here
// : x(0), y(0), z(0)
{
x=0;
y=0;
z=0;
}
void Point::set_values (int a, int b, int c){
x=a;
y=b;
z=c;
}
void Point::affichervaleurs(){
cout << "X = " << x << endl;
cout << "Y = " << y << endl;
}
Now let's consider the Carre.h file
class Carre:public Point{
public:
// IT'S DEFINITION WHICH IS USED WHEN YOU CALL THE FUNCTION
int Aire (){};
// IT'S DEFINITION WHICH IS USED WHEN YOU CALL THE FUNCTION
void affichercar(){};
};
Now I'm starting answering your questions:
The problem is, when I call this line :
cout << MonCarre.Aire() << endl;
it returns a strange number.
Yes, it's a strange number returned by the function defined in the Carre.h (not Carre.cpp) file.
I'm sure that the problem is here :
int Aire (){
return (x * y);
};
No, as you see, it's not.
Like the x and y are not linked with the superclass.
In fact they are.
Can someone help me how to access my x and y values from my superclass in my class Carre ?
You already have this access. It's okay to use protected members from base class in derived class in case of public inheritance.
Other strange thing this line doesn't appear : MonCarre.affichercar();
You mean it does nothing? But it's in perfect consistency with how you implement (define) the function in Carre.h (not Carre.cpp) file.
So let me change the code to make it work.
Carre.h
#ifndef Carre_H
#define Carre_H
#include "Point.h"
class Carre:public Point{
public:
int Aire (); // now it's declaration
void affichercar(); // now it's declaration
};
#endif
Carre.cpp
#include <iostream>
using namespace std;
int Carre::Aire (){
return (x * y);
}
void Carre::affichercar(){
cout << "Coordonnees X:" << x << endl;
}
I didn't compile this for myself, but the whole idea should be clear.
access my x and y values
From within Carre, you simply access them by 'x' and 'y'.
From outside, you cannot as they are not public.

Passing Pointers to Classes in C++

I am trying to pass a pointer into my classes function, have it incremented, and have the variable retain it's value using pointers. Heres my code, it doesnt increment.
#include "stdafx.h"
#include <iostream>
using namespace std;
class test
{
public:
int addTo();
test(int * currentY);
private:
int y;
};
test::test(int * currentY):
y(*currentY)
{
}
int test::addTo()
{
y++;
return 0;
}
int main ()
{
for (;;)
{
int pointedAt = 1;
int * number = &pointedAt;
test t(number);
t.addTo();
cout <<*number;
char f;
cin >>f;
}
}
This should do it:
#include "stdafx.h"
#include <iostream>
using namespace std;
class test
{
public:
int addTo();
test(int * currentY);
private:
int *y;
};
test::test(int *currentY):
y(currentY)
{}
int test::addTo()
{
++*y;
return 0;
}
int main ()
{
for (;;)
{
int pointedAt = 1;
test t(&pointedAt);
t.addTo();
cout << pointedAt;
}
}
You have to store a pointer to the integer, so it refers to the same address as the original variable.