Changing Function Access Mode in Derived Class
Yes, changing the access mode in derived classes is legal.
This is similar in form but different in intent to the Non-Virtual Interface idiom. Some rationale is given here:
The point is that virtual functions exist to allow customization; unless they also need to be invoked directly from within derived classes' code, there's no need to ever make them anything but private.
As to why you would actually make something public
in base but private
in derived without private
or protected
inheritance is beyond me.
Change the visibility of a function in a Derived class
This is how I understand your problem. You require that if your derived class wants to fall back to the implementation in the base class it has to state that explicitly. consider this solution.
class Airplane {
public:
virtual ~Airplane() {}
virtual void fly(int destination) = 0;
};
inline void Airplane::fly(int destination) // implement pure virtual function!
{
std::cout << "Flyed defaultly to " << destination << std::endl;
}
class ModelA: public Airplane {
public:
virtual void fly(int destination) { Airplane::fly(destination); }
virtual ~ModelA() {}
};
The trick here is to provide the body for the pure virtual function. It is legal, and occasionally useful, in C++.
C++ Use derived class method to change base class data member
You must define name as static std::string
because modifying non-static members of an object change them only for that object.
Access specifier while overriding methods
Yes you can, but it "doesn't grok".
Take a look at Overriding public virtual functions with private functions in C++
Can we access protected member functions of base class in derived class?
Yes, the derived class can access protected members, whether those members are data or functions. But in your code, it's main
which is attempting to access setWidth
and setHeight
, not Rectangle
. That's invalid just like using width
and height
from main
would be.
An example of the derived class using the protected member functions:
class Rectangle: public Shape {
public:
int getArea() const {
return (width * height);
}
void setDimensions(int w, int h) {
setWidth(w);
setHeight(h);
}
};
Or if you really want Rectangle
to let anyone else use the functions, you can use the access Rectangle
has to make them public
members of Rectangle
instead of protected
:
class Rectangle: public Shape {
public:
using Shape::setWidth;
using Shape::setHeight;
int getArea() const {
return (width * height);
}
};
Why private virtual member function of a derived class is accessible from a base class
First, as @Eljay notes - printImpl()
is a method, albeit virtual, of the Base
class. So, it's accessible from the base class. Derived
merely provides a different implementation of it. And the whole point of virtual functions is that you can call a subclass' override using a base class reference or pointer.
In other words, private
only regards access by subclasses; it's meaningless to keep something private
from a class' base class: If a method is at all known to the base class, it must be a method of the base class... a virtual method.
Having said all that - note that the Derived
version of printImpl()
is effectively inaccessible from print()
- when it's invoked within the base class constructor. This is because during that call, the constructed vtable is only that of Base
, so printImpl
points to Base::printImpl
.
I thought private member functions could be called ONLY from the member functions of the same class
And indeed, print()
is a member of Base
, which invokes printImpl()
- another method of Base
.
C++ change access for member function with overloading
As Armen and Jarod pointed out, using
will bring all fun
's in the derived class. Jarod though had a great idea: delete
the evil function! Combining their answers I got this:
template<typename T1, typename T2>
class Derived : private Base<T1>
{
public:
using Base<T1>::fun;
void fun(float) = delete;
};
which does exactly what I wanted originally!
How to change the a vector from a base class via a derived class function
Your problem is that you defined functions get_Points_Within_Shape()
and get_Points_On_Perimeter()
as returning copies of internal vectors. Instead you should define them to return references to internal vectors. Also, usually there are 2 versions for such functions: usual function returning usual vector and const
function returning const
vector. I.e. your declaration should look something like this:
std::vector<Point>& get_Points_Within_Shape() {return points_within_shape_;}
const std::vector<Point>& get_Points_Within_Shape() const {return points_within_shape_;}
Related Topics
Why Does the Enhanced Gcc 6 Optimizer Break Practical C++ Code
Passing Integers as Constant References Versus Copying
What Are the Differences Between Overriding Virtual Functions and Hiding Non-Virtual Functions
Why Don't the Std::Fstream Classes Take a Std::String
I Want to Convert Std::String into a Const Wchar_T *
How to Return a Char Array from a Function
C++ Typedef Interpretation of Const Pointers
What Is the Type of a String Literal in C++
Meaning of (Number) & (-Number)
Hash an Arbitrary Precision Value (Boost::Multiprecision::Cpp_Int)
Where Do I Find the Definition of Size_T
Lambda Expressions as Class Template Parameters
How to Identify the File Content as Ascii or Binary
Getline() Does Not Work If Used After Some Inputs
Identifier Not Found Error on Function Call
Preincrement Faster Than Postincrement in C++ - True? If Yes, Why Is It
Faq: Why Does Dynamic_Cast Only Work If a Class Has at Least 1 Virtual Method