Is there any way to call a parent's function from a child class but within the function use data from the child instead of the parent eg.
#include <stdio.h>
class Base{
public:
int i=10;
virtual void print(){
printf("%i", i );
}
};
class Derived : public Base {
public:
int i = 20;
void print(){
Base::print();
}
};
int main (){
Derived I;
I.print();
}
So I want this to print 20 instead of 10 basically.
EDIT ::::::::::::
What I am actually trying to do is use a base class that defines multiple functions
virtual int OnExecute();
virtual bool OnInit();
virtual void OnEvent(SDL_Event* Event);
virtual void OnLoop();
virtual void OnRender();
virtual void OnCleanUp();
Then have a derived class overload all of those functions exept OnExectue() which looks like this
if (OnInit() == 0 ){
return -1;
}
SDL_Event Event;
while (isRunning){
while (SDL_PollEvent(&Event)){
OnEvent(&Event);
}
OnLoop();
OnRender();
}
OnCleanUp();
So is there a more simplistic way where i won't be having to define 5 extra functions to pass function pointers.
You can do this if you provide access to the data through a virtual member function.
#include <stdio.h>
class Base{
public:
int i=10;
virtual void print(){
printf("%i", this->getI() );
}
virtual int getI()
{
return i;
}
};
class Derived : public Base {
public:
int i = 20;
void print(){
Base::print();
}
virtual int getI()
{
return i;
}
};
int main (){
Derived I;
I.print();
}
You need a dynamic_cast , Like this:
#include <iostream>
#include <stdio.h>
class Derived;
class Base{
public:
int i=10;
virtual void print();
};
class Derived : public Base {
public:
int i = 20;
void print(){
Base::print();
}
};
void Base::print(){
Derived* p = dynamic_cast<Derived*>(this);
if(p) {
//derived
printf("%i\n", p->i);
}else{
//base
printf("%i\n", i);
}
}
int main (){
Derived I;
I.print();
}
Related
I need to assign x = 2000 to x of B via object a of A
Here B is the derived class i.e inherits the class A.
class A
{
public:
int x, y;
void print()
{
cout<<endl<<"print() of A";
}
virtual void display()
{
cout<<endl<<"display() of A";
}
};
class B: public A
{
public:
int x, z;
void display()
{
cout<<endl<<"display() of B";
}
void print()
{
cout<<endl<<"print() of B";
}
};
Found the answer by doing the following:
((B *)aptr)->x = 2000;
In C++, polymorphism is implemented via virtual functions. If you need to change something in a derived class through a pointer or reference to its base type, you need a virtual function. (Well, technically, you don't; you could cast to the derived type, but that's an admission of design failure).
It can be done by creating virtual function in base class which effect to call function of derive class for initialization.
#include<iostream>
#include<stdio.h>
using namespace std;
class A
{
public:
int x, y;
void print()
{
cout<<endl<<"print() of A";
}
virtual void display()
{
cout<<endl<<"display() of A";
}
virtual void setX(int a)
{
}
};
class B: public A
{
public:
int x, z;
void display()
{
cout<<endl<<"display() of B";
}
void print()
{
cout<<endl<<"print() of B";
}
void setX(int a)
{
x=a;
}
};
int main()
{
A *ptr;
B b;
ptr=&b;
ptr->setX(2000); ///using pointer object of class A
cout<<b.x;
}
I think it will help you :)
I have this abstract class in C++:
class A {
public:
A(int size);
virtual void doSomething(int inputSize) = 0;
protected:
virtual bool checkSize(int inputSize);
private:
int size;
}
A::A(int size) : size(size){}
bool A::checkSize(int inputSize) { return size == inputSize; }
Now, what I want to guarantee is that for each class B derived from A doSomething begins like this:
class B : public A{
void doSomething(int inputSize);
}
void B::doSomething(int inputSize){
if(!checkSize(inputSize)){
std::cerr<<"wrong inputSize!"<<std::endl;
return;
}
//do something...
}
How can I guarantee that each derived class from A implements doSomething starting in that way?
You split doSomething into two parts:
class A {
public:
void doSomething(int inputSize) {
if (!checkSize(inputSize)){
std::cerr << "wrong inputSize!" << std::endl;
return;
}
doSomething_(inputSize);
}
protected:
virtual void doSomething_(int) = 0;
};
And in B you only implement doSomething_.
You may do the check in A directly, something like:
class A {
public:
A(int size);
virtual ~A() = default;
void doSomething(int inputSize)
{
if (!checkSize(inputSize)){
std::cerr<<"wrong inputSize!"<<std::endl;
return;
}
doSomethingWithChekedSize(inputSize);
}
protected:
virtual void doSomethingWithChekedSize(int inputSize) = 0;
virtual bool checkSize(int inputSize);
private:
int size;
};
if I have the following code:
class A
{
public:
virtual void Yo();
}
class B : public A
{
public:
virtual void Yo() override;
}
Is there a way to force B to implement method Yo in A? Like an interface or more specifically (in this case) an abstract?
My full code is here:
BaseObject.h
#pragma once
namespace Game
{
namespace Model
{
namespace Graphic
{
class BaseObject
{
public:
int Width;
int Height;
float X;
float Y;
float Z;
virtual void SetUp() = 0;
virtual void Reset() = 0;
};
}
}
}
Player.cpp
#include "pch.h"
#include "Abstract\BaseObject.h"
using namespace Game::Model::Graphic;
class Player : public BaseObject
{
public:
Player();
~Player();
//virtual void SetUp();
//virtual void Reset() override;
};
in A make Yo pure virtual
virtual void Yo() = 0;
a complete example
#include <iostream>
struct A
{
virtual void Yo() = 0;
};
struct B : A
{
virtual void Yo() { std::cout << "I'm B\n"; }
};
int main()
{
B b;
b.Yo();
return (0);
}
if B doesn't implement Yo this will not compile.
The override keyword it's not needed in this case.
You have to take pointer of the base class to implement virtual functions in C++. This seems quite obvious. But i think we have missed that. The most basic example is given below.
#include <iostream>
using namespace std;
class base {
public:
virtual void vfunc() {
cout << "This is base's vfunc().\n";
}
};
class derived1 : public base {
public:
void vfunc() {
cout << "This is derived1's vfunc().\n";
}
};
int main()
{
base *p, b;
derived1 d1;
// point to base
p = &b;
p->vfunc(); // access base's vfunc()
// point to derived1
p = &d1;
p->vfunc(); // access derived1's vfunc()
return 0;
}
So I have two classes. One has only purely virtual functions. THe other implements those functions and is derived from the first class.
I get that i cant instantiate the first class. But when I try to create an object of the second class it fails as well.
This is how my second class looks in general:
class SecondClass : public FirstClass
{
public:
SecondClass();
virtual ~SecondClass(void);
void Foo();
void Bar();
}
Implementation:
SecondClass::SecondClass()
{...}
SecondClass::~SecondClass(void)
{...}
void SecondClass::Foo()
{...}
void SecondClass::Bar()
{...}
This how I instantiate it and get the Error:
SecondClass mSecClass;
Where am I going wrong here?
FirstClass.h
class FirstClass
{
public:
FirstClass(void);
virtual ~FirstClass(void);
virtual void Foo() = 0;
virtual void Bar() = 0;
};
You need to define the ~FirstClass() destructor and leave out its constructor
class FirstClass
{
public:
virtual ~FirstClass(void) {} // or use C++11 = default syntax
virtual void Foo() = 0;
virtual void Bar() = 0;
};
class SecondClass : public FirstClass
{
public:
SecondClass();
virtual ~SecondClass(void);
void Foo();
void Bar();
};
SecondClass::SecondClass() {}
SecondClass::~SecondClass(void) {}
void SecondClass::Foo() {}
void SecondClass::Bar() {}
int main()
{
SecondClass mSecClass;
}
Live Example.
Define every function you declare, except for pure virtuals(virtual void foo() = 0).
try the below code:
#include<iostream>
using namespace std;
class FirstClass
{
public:
FirstClass()
{
//
}
virtual ~FirstClass();
virtual void Foo();
virtual void Bar();
};
FirstClass::~FirstClass()
{
//
}
void FirstClass::Foo()
{
//
}
void FirstClass::Bar()
{
//
}
class SecondClass : public FirstClass
{
public:
SecondClass();
virtual ~SecondClass(void);
void Foo();
void Bar();
};
SecondClass::SecondClass(){
//
}
SecondClass::~SecondClass(void)
{//
}
void SecondClass::Foo()
{//
}
void SecondClass::Bar()
{//
}
int main()
{
SecondClass name;
return 0;
}
What happens when a class inherits from multiple abstract classes when 2 or more of them have a function with the same name, return type, and arguments?
Assuming all functions here are virtual
Thanks
class C inherits from A and B at the same time and both A & B have virtual void func(int h);
If this is what you mean,
#include <iostream.h>
class A
{
public:
virtual void a_show()=0;
virtual void show()
{
cout<<"A";
}
};
class B
{
public:
virtual void b_show()=0;
virtual void show()
{
cout<<"B";
}
};
class C : public A, public B
{
virtual void a_show()
{}
virtual void b_show()
{}
};
void main()
{
C s;
s.show();
}
The code gives an error with VC++ like
error C2385: 'C::show' is ambiguous
You need to declare show like this :
#include <iostream.h>
class A
{
public:
virtual void a_show()=0;
virtual void show()
{
cout<<"A";
}
};
class B
{
public:
virtual void b_show()=0;
virtual void show()
{
cout<<"B";
}
};
class C : public A, public B
{
public:
virtual void a_show()
{}
virtual void b_show()
{}
void show()
{
cout<<"C";
}
};
void main()
{
C s;
s.show();
}
This sure will give C
C++ also allows to pick an inherited virtual member function (IVMF) as well, so you don't need to override an IVMF. Borrowing the example from mihsathe, we can do the following:
class C : public A, public B {
public:
virtual void a_show() { }
virtual void b_show() { }
using B::show;
// using A:show; // If you want to use show() from A
};