When a class inherits another publicly, then shouldn't all virtual functions get rewritten?
Consider the code --
class A {
private:
vector<int> v;
public
virtual int something() {
cout << "A" << endl;
return v.size();
}
}
class B : public A {
private:
priority_queue<int> v;
public
int something() {
cout << "B" << endl;
return v.size();
}
}
Now, when I call the function something() on an object b of class B by executing the statement b.something(), I get the output A. Why is this?
Corrected the errors (see http://codepad.org/ssivYhWb)
i.e.
#include <iostream.h>
class A {
private:
vector<int> v;
public:
virtual int something() {
cout << "A" << endl;
return v.size();
}
};
class B : public A {
private:
priority_queue<int> v;
public:
int something() {
cout << "B" << endl;
return v.size();
}
};
int main() {
B b;
b.something();
return 0;
}
It returns B - as expected
Related
i need to call the overidden function inside B class through C class but it gives "Base" as a output rather than "Derived". when I call this code part classC.getTargetB().example(); i need to get "Derived" as a output. how can i reach my goal in the code.
class A {
public:
A() {};
virtual void example() { std::cout << "Base" << std::endl; }
};
class B :public A {
protected:
std::string name;
public:
B() {};
B(std::string n) { name = n; }
void example() override { std::cout << "Derived" << std::endl; }
};
class C {
protected:
std::vector<std::shared_ptr<A>> ptr;
std::shared_ptr<A> targetBobject;
public:
void setB(std::shared_ptr<A> target) {
targetBobject = target;
}
A getTargetB() {
return *targetBobject;
}
};
int main()
{
A example;
std::vector<std::shared_ptr<A>> parent;
C classC;
std::shared_ptr<B> ptr1 = std::make_shared<B>("B1");
std::shared_ptr<B> ptr2 = std::make_shared<B>("B2");
parent.push_back(std::move(ptr1));
parent.push_back(std::move(ptr2));
//parent.at(0)->example(); this gives 'Derived' as a output
classC.setB(parent.at(0));
classC.getTargetB().example();// gives 'Base' as a output
}
How do I make a static method inaccessible from a derived class?
I have an inheritance of classes of the following type Square
from which rectangle and rhombus are derived. Then from the rectangle and rhombus a parallelogram is derived. Then from the parallelogram the trapezoid is derived.
I read all these objects (square, rectangle, rhombus, parallelogram, trapeze) in an object vector, but in the end I want to show how many of each I have read. I want to do this through static functions for each class.
Here is the code:
class A
{
protected:
static int n;
public:
A();
static void numberA()
{
cout << n;
}
};
int A::n;
A::A(){ n++; }
class B:public A
{
protected:
static int n;
public:
B();
static void numberB()
{
cout << n;
}
};
int B::n;
B::B(){ n++; }
int main()
{
A a;
B b;
A::numberA();
cout << endl;
B::numberB();
return 0;
}
output:
2
1
And I want 1, 1 because it is one object of class A and one of class B.
I also tried to make the inheritance private or protected but for nothing.
The problem is that the constructor of the derived class will call the base class constructor too.
An easy fix could be to decrement the count of the base class in the derived constructor.
#include <iostream>
class A
{
protected:
inline static int n_{}; // Since C++17
public:
A()
{
++n_;
};
static int number()
{
return n_;
}
};
class B : public A
{
protected:
inline static int n_{};
public:
B()
{
++n_;
--A::n_; // Decrease base count
}
static int number()
{
return n_;
}
};
class C : public B
{
protected:
inline static int n_{};
public:
C()
{
++n_;
--B::n_; // Decrease base count
}
static int number()
{
return n_;
}
};
class D : public A
{
protected:
inline static int n_{};
public:
D()
{
++n_;
--A::n_; // Decrease base count
}
static int number()
{
return n_;
}
};
int main()
{
A a;
B b;
C c;
D d;
std::cout << A::number() << '\n' << B::number() << '\n'
<< C::number() << '\n' << D::number() << '\n';
return 0;
}
I am trying to deal with abstract class argument, trying to figure out what to do, after running this code below getting the error : "In function 'int main()': 67:12: error: expected primary-expression before ')' token"
#include <iostream>
#include <string>
using namespace std;
class base1 {
protected:
int i;
public:
base1(int x) {
i=x;
cout << "Constructing base1\n";
}
virtual ~base1() {
cout << "Destructing base1\n";
}
};
class derived: public base1 {
int j;
public:
derived(int x, int y): base1(y){
j=x;
cout << "Constructing derived\n";
}
~derived() {
cout << "Destructing derived\n";
}
void show() {
cout << i << " " << j << " " << "\n";
}
};
class Isolver
{
public :
Isolver(){};
virtual ~ Isolver(){};
virtual void x(base1* pboard)=0;
};
class vr:public Isolver
{
void x(base1* pboard)
{
cout << "My virtual fun and base constructor are not working\n"<<endl;
};
};
int main()
{
vr obj;
obj. x(10, );
derived ob(3,4);
ob.show();
return 0;
}
Like Paul Rooney pointed out the comma inside obj.x(10,) should not be there because with the comma the compiler is expecting two arguments and since there is nothing after the comma it shows an error.
This is what you need to do inside main:
vr obj;
base1 b(1);
obj. x(&b);
or this:
vr obj;
derived ob(3,4);
obj. x(&ob);
ob.show();
and inside class vr
class vr:public Isolver
{
public:
void x(base1* pboard)
{
cout << "My virtual fun and base constructor are not working\n"<<endl;
}
};
#include <iostream>
using namespace std;
class A {
private:
int nVal;
public:
void Fun()
{
cout << "A::Fun" << endl;
}
void Do()
{
cout << "A::Do" << endl;
}
};
class B :public A {
public:
virtual void Do()
{
cout << "B::Do" << endl;
}
};
class C :public B {
public:
void Do()
{
cout << "C::Do" << endl;
}
void Fun()
{
cout << "C::Fun" << endl;
}
};
void Call(B& p)
{
p.Fun(); p.Do();
}
int main() {
C c; Call(c);
return 0;
}
In the above code, the output is
A::Fun
C::Do
But I cannot understand. in the function call(B& p), the reference p should refer to c from C class, so why the output is not
C::Fun
C::Do
When announcing a a basis class reference to a derived class, is the different basis class making any sense?
The problem is that A::Fun is not marked as virtual, therefore it doesn't find C::Fun at run-time. You should mark it as such:
class A {
private:
int nVal;
public:
virtual void Fun()
{
cout << "A::Fun" << endl;
}
// ...
wandbox example
This is a great example of an error that could be caught at compile-time thanks to the override specifier:
class C :public B {
public:
void Do() override
{
cout << "C::Do" << endl;
}
void Fun() override
{
cout << "C::Fun" << endl;
}
};
Would print:
error: 'void C::Fun()' marked 'override', but does not override
void Fun() override
^~~
wandbox example
I was reading about delegation and I wanted to able to call with a base class a any function pass as parameter depending on the event, I did it this way and it works but I am not sure if it is the proper way and if it is portable like this.
class base {
public:
typedef void (base::*methodPTR)();
methodPTR pfn;
void setMethod(methodPTR fn)
{
pfn = fn;
}
void run(){
if(pfn) (this->*pfn)();
}
};
class a : public base {
public:
void fn()
{
cout<<"from class a!"<<endl;
}
};
class b : public base
{
a ob;
public:
void init()
{
ob.setMethod(static_cast<base::methodPTR>(&b::fn));
}
void fn()
{
cout << "from class b!" << endl;
}
void test()
{
ob.run();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
b x;
x.init();
x.test();
return 0;
}
You are calling the member function fn of class b with an instance of class a which will cause undefineded behaviour if you access data memebers of your class.
Replace your classes a and b with this to see the magic :
class a : public base {
int j;
public:
a()
{
j = 42;
}
void fn()
{
cout<<"from class a!"<<endl;
}
};
class b : public base
{
int i;
a ob;
public:
void init()
{
i = 5;
ob.setMethod(static_cast<base::methodPTR>(&b::fn));
}
void fn()
{
cout << "from class b!" << endl;
cout << "i = " << i << endl;
}
void test()
{
ob.run();
}
};