OOP Problems C++ [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
what is wrong? I think, it's correctly
class A{
public:
virtual bool isGun() = 0;
virtual bool isArmor() = 0;
};
class B: public A{
public:
virtual bool isGun(){ return true; }
};
int main(){
B* b = new B;
}
A::isArmor() - defined is other class
I realy need to define all pure virtual functions in all child-classes?

what is wrong?
You haven't defined isArmor in B so you can't create any instances of B.
I realy need to define all pure virtual functions in all child-classes?
Yes. That is what a pure virtual function means. If you don't want to have to define a function in all child-classes then don't make it pure virtual.

You have to override the pure virtual function in your subclass otherwise your subclass will become abstract class and hence we cannot create object of abstract class.
If you do not want isArmor function in subclass to expose, override it as private so that client of B cannot use it.
class B : public A{
private:
virtual bool isArmor() { return true; } // Like this
public:
virtual bool isGun(){ return true; }
};

Related

How do I create a new object from an array which holds different objects in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a base class "A" and many derived classes, say "B", "C", "D".
I need to have an array which holds these objects (or pointers). I have a function which will take an index into this array and should create one of the objects (make new object) in the array. How do I do this ? Following is a snippet, showing just wrappers below.
class A
{
};
class B: public A
{
};
class C: public A{
};
class D: public A{
};
static A anArray[] = {B, C,D };
int main() {
`A anObject = myFunction(2);`
}
In the above code, when I call myFunction, I should be able to get a new D
There are many ways to achive this,
here's one way:
#include <memory>
class A {
public:
virtual ~A() = default; // important
};
class B: public A { };
class C: public A{ };
class D: public A{ };
static std::unique_ptr<A> anArray[] = {
std::make_unique<B>(),
std::make_unique<C>(),
std::make_unique<D>()
};

Why is this pure virtual method not compiling? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I use a pure virtual method as shown in the code below.
#include <iostream>
using namespace std;
class Advertisment
{
public:
vitual void price (int Uchrg, int no_of_unt) = 0;
{
}
};
class TVadvertisment : public Advertisment
{
public:
void price (int Uchrg, int no_of_unt)
{
int adPrice = Uchrg * no_of_unt;
cout << "Advertisment Price: " << adPrice;
}
};
int main()
{
TVadvertisment T;
T.price(1000, 60);
return 0;
}
As I know a pure virtual function will be declared as virtual void display() = 0;. But the Code::Blocks compiler show an error because of this = 0. Without that it will compile successfully.
And also I didn't use pointers to call methods of derived class.
Your function is pure virtual, which means the method is virtual and not even implemented in the base class (= 0).
So you have to delete the block after it.
It has to be:
virtual price(int Uchrg, int no_of_unt) = 0;
without the { }.
Virtual means, that classes that inherit from a base class can override the method and called via the base class interface. If a class has a pure virtual method, the class is abstract and this function needs to be overridden by classes that inherit from that base class to be instantiated.
In short:
Virtual:
virtual price(int Uchrg, int no_of_unt)
{
// Implementation
}
Has an implementation is the base class. Sub classes does not need to override, but they can. The class is not abstract and can be instanciated.
Pure virtual:
virtual price(int Uchrg, int no_of_unt) = 0; // No implementation
There is no implementation, the sub classes must override this to be not abstract.
Call a virtual method via base class:
Base* pBase = new Derived;
pBase->fun();
The method is called via the interface of the base class, but it will be the derived class' method.

Inheritance of private members [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
When asked if private members of class B are inherited by D, a class derived from B, people emphatically say: yes they are inherited but not accessible directly, only indirectly for instance via public methods in the base class. OK, but what is the difference between not being inherited and not being directly accessible? For instance class X (NOT derived from B) also has access to private members of B via public methods of B, even though X doesn't inherit anything from B.
what is the difference between
1) not being inherited and
2) being inherited and not being directly accessible.
what is not clear?
The difference is in the class layout.
struct B {
private: char buf[1024];
};
struct D : B { };
Here, sizeof(D) >= sizeof(B). It's still there, i.e. it's clearly inherited.
If the base class has virtual functions that can be overriden by the derived class, clearly this is an important difference to a class having access to a base class instance but not inheriting from it:
class B
{
public:
virtual ~B() {}
int get_a() const
{
do_something();
return a;
}
virtual void do_something() const
{
std::cout << "In B\n";
}
private:
int a{};
};
class D : public B
{
virtual void do_something() const
{
std::cout << "In D\n";
}
};
int main()
{
D d;
d.get_a();
}
If the base class does not have any virtual functions, then the usefulness of inheriting from it is less clear. In fact, it is perhaps best not to inherit from it (at least publicly) as the lack of a virtual destructor could cause some problems, and it should be made a member variable instead.

Using Derived function from base class [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a class test_d which publicly inherits class test_b. The class test_d has a function getValues() which I need to call using an object of class test_b. I tried using dynamic_cast and reinterpret_cast but it didn't work. Is there any other way to do that?
class test_b {
// this is the base class
};
class test_d : public test_b {
// this is the derived class
public int getValues() const; // this is the function I need to use.
};
test_b* objB = new test_d;
dynamic_cast<test_d*>(objB)->getValues(); // this is what I am doing.
In your interface you should declare your method as pure virtual function and then in your derived class you should write an implementation
class test_b
{
public:
virtual int getValues() const = 0;
};
class test_d : public test_b
{
public:
virtual int getValues() const
{
return m_value;
}
};
And from somewhere of your main():
test_b* objB = new test_d;
objB->getValues();
this is the basics of OOP: the interface and an implementation of the interface

What is the use of virtual class and abstract class in C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I understood what is virtual function and pure virtual function but, what is the use of virtual function in C++. Can I get a more apt example for this concept in which virtual function can be used?
The example given for this is
1.Shape a base class
2.Rectangle and square be the derived class
My question is what is the need for shape derived class in first place?
Why cant we directly uses rectangle and square class directly
You can use virtual function to achieve runtime polymorphism.
You can use virtual functions when you want to override a certain behavior(read method) for your derived class than the one implemented for the Base class and you want to do so at run-time through an pointer to Base class.
Example:
#include <iostream>
using namespace std;
class Base {
public:
virtual void NameOf(); // Virtual function.
void InvokingClass(); // Nonvirtual function.
};
// Implement the two functions.
void Base::NameOf() {
cout << "Base::NameOf\n";
}
void Base::InvokingClass() {
cout << "Invoked by Base\n";
}
class Derived : public Base {
public:
void NameOf(); // Virtual function.
void InvokingClass(); // Nonvirtual function.
};
// Implement the two functions.
void Derived::NameOf() {
cout << "Derived::NameOf\n";
}
void Derived::InvokingClass() {
cout << "Invoked by Derived\n";
}
int main() {
// Declare an object of type Derived.
Derived aDerived;
// Declare two pointers, one of type Derived * and the other
// of type Base *, and initialize them to point to aDerived.
Derived *pDerived = &aDerived;
Base *pBase = &aDerived;
// Call the functions.
pBase->NameOf(); // Call virtual function.
pBase->InvokingClass(); // Call nonvirtual function.
pDerived->NameOf(); // Call virtual function.
pDerived->InvokingClass(); // Call nonvirtual function.
}
Output will be:
Derived::NameOf
Invoked by Base
Derived::NameOf
Invoked by Derived