sum of number using private mode of inheritance in c++ - c++

How to make this type of program. How to do that can someone say as soon as possible a simple program will also work like sum of two number with private mode of inheritance.
output need to be look like this
I make this program with public mode of inheritance and I want this program in private mode of inheritance.
I need to use class child : private base1, private base2
#include<iostream>
using namespace std;
class base1
{
public:
int a;
void geta()
{
cout<<"enter value of a: ";
cin>>a;
}
};
class base2
{
public:
int b;
void getb()
{
cout<<"enter value of b: ";
cin>>b;
}
};
class child : public base1, public base2
{
public:
void sum()
{
cout<<"Sum of a and b is: "<<a + b;
}
};
int main()
{
child c;
c.geta();
c.getb();
c.sum();
return 0;
}

Maybe you want this:
...
class child : private base1, private base2
{
public:
void sum()
{
cout << "Sum of a and b is: " << a + b;
}
void getaandb()
{
geta();
getb();
}
};
int main()
{
child c;
c.getaandb();
c.sum();
return 0;
}

Related

How to make 2 base classes to use the same property of their base class?

#include <iostream>
class Base {
public:
int a;
Base() : a(0) {};
};
class Inherited1 : public Base {
public:
void add() {
a++;
}
};
class Inherited2 : public Base {
public:
void sub() {
a--;
}
};
class Combined : public Inherited1, public Inherited2 {
//public:
// int& a;
//
// Combined() : a(Inherited1::a) {};
};
int main() {
Combined obj;
obj.add();
obj.add();
obj.sub();
std::cout << dynamic_cast<Inherited1*>(&obj)->a << ' '; // -> 2
std::cout << dynamic_cast<Inherited2*>(&obj)->a; // -> -1
return 0;
}
I want to somehow force add() method in Combined class to use the same variable as sub() method without changing Base, Inherited1 or Inherited2 classes, so my program would print out "1 1".
Is it even possible in that example?
I'm curious because I'm trying to create something like std::iostream, just for fun.

Static method inaccessible from a derived class

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;
}

Accessing Inherited Functions

In multiple inheritance,where all the base class contains same function name with different functionality, we can access the protected function from particular base class using "::" scope resolution operator.
However, I tried something else. I created the objects of the base class in inside the child class. And tried calling the function using through object of that particular class.
But I was getting the following compiler error:
"‘void A::func(int&)’ is protected within this context."
Please let me know where did i go wrong.
#include <iostream>
using namespace std;
class A
{
protected:
void func(int & a)
{
a = a * 2;
}
};
class B
{
protected:
void func(int & a)
{
a = a * 3;
}
};
class C
{
protected:
void func(int & a)
{
a = a * 5;
}
};
class D : public A,public B,public C {
public:
int a;
A a_val;
B b_val;
C c_val;
void update_val(int new_val)
{
a = new_val;
a_val.func(a);
b_val.func(a);
c_val.func(a);
}
void check(int);
};
void D::check(int new_val)
{
update_val(new_val);
cout << "Value = " << a << endl;
};
int main()
{
D d;
int new_val;
cin >> new_val;
d.check(new_val);
}
If you want to keep your code with the base classes as having independent functionality and still remaining protected the easiest way to resolve your issue is by slightly changing the name of your protected functions and adding a public function that calls the protected members: See these class declarations for example:
class A {
public:
void func( int& a ) {
func_impl( a );
}
protected:
void func_impl( int& a ) {
a = a * 2;
}
};
class B {
public:
void func( int& b ) {
func_impl( b );
}
protected:
void func_impl( int& b ) {
b = b * 3;
}
};
class C {
public:
void func( int& c ) {
func_impl( c );
}
protected:
void func_impl( int& c ) {
c = c * 5;
}
};
class D : public A, public B, public C {
public:
int a;
A a_val;
B b_val;
C c_val;
void update_val( int val ) {
a = val;
a_val.func( a );
b_val.func( a );
c_val.func( a );
}
void check( int );
};
void D::check( int val ) {
update_val( val );
std::cout << "Value = " << a << std::endl;
}
This provides a nice public interface to call the protected member functions. This also resolves the issue of accessing the protected members. When I run your program and input a value of 5 it returns a result of 150 and works as expected.
This snippet should show you how inheritance works and when you can and can not access protected members:
class DerivedA : public Base {
public:
Base b;
void call_message() {
b.message(); // Protected Member of Base class can not be accessed
}
};
class DerivedB : public Base {
public:
void call_message() {
message(); // This works without problem!
}
};
Just as I did above one way to resolve this is by adding a public interface caller to the protected implementation.
class Base {
public:
void message() {
message_impl();
}
protected:
void message_impl() {
std::cout << "This is a protected member of Base\n";
}
};
Now you can do this:
class DerivedA {
public:
Base b;
void call_message() {
b.message(); // Accessible through public interface.
}
};
When you are in your derived class, it has access to its own ancestor methods. But it doesn't have access to your variables member protected and private methods and variables.
Redesign your code, you are trying things and contorting the other classes design for bad reasons. Francis' code is a good solution, but D doesn't need to inherit from anything.
If you don't want to create another function, you can do something like this:
#include <iostream>
using namespace std;
class A
{
protected:
void func(int & a)
{
a = a * 2;
}
};
class B
{
protected:
void func(int & a)
{
a = a * 3;
}
};
class C
{
protected:
void func(int & a)
{
a = a * 5;
}
};
class D : public A,public B,public C {
public:
int a;
void update_val(int new_val)
{
a = new_val;
this->A::func(a);
this->B::func(a);
this->C::func(a);
}
void check(int);
};
void D::check(int new_val)
{
update_val(new_val);
cout << "Value = " << a << endl;
};
int main()
{
D d;
int new_val;
cin >> new_val;
d.check(new_val);
}
This works because, this refers to the current instance of class D, and it already inherits class A, class B, class C. So you can directly access the protected functions of the respective classes.
Remember: It will not work if you have not inherited the classes.

cpp store the objects in an array of pointer

I am trying to do something like this with c++.
void showContensofArray(void *data[])
{
//In this function have to display the values of respective objects.
// Any ideas how do I do it?
}
int main(){
A phew(xxx,abcdefg); //object of class A
B ball(90),ball2(88); //object of class B
void *dataArray[2];
dataArray[0] = &ph1;
dataArray[1] = &ball;
showContentsofArray(dataArray); //function
}
If you want to treat the objects in the data[] generically (i.e by calling a common function on them to extract a description or values) then define a class hirachy for your objects and in your showContentsofArray function call virtual methods on your (common base class) object pointers.
This is a textbook example of Polymorphism:
"polymorphism allows values of different data types to be handled using a uniform interface."
In the example below the base class BaseObject defines the uniform interface.
class BaseObject {
virtual string description() { return "Base object"; }
virtual bool bounces() { return false; }
}
class B : public BaseObject {
string description() { return "Im a B object" }
bool bounces() { return true; }
}
class A : public BaseObject {
string description() { return "Im an A object" }
}
void showContensofArray(BaseObject* data[], int size) {
for (int i=0; i<size; i++) {
cout << data[i]->description();
if (data[i]->bounces())
cout << "I bounce!";
}
}
int main() {
A phew(xxx,abcdefg); //object of class A
B ball(90),ball2(88); //object of class B
BaseObject* dataArray[2];
dataArray[0] = &ph1;
dataArray[1] = &ball;
showContentsofArray(dataArray);
}
Will output:
Im an A object
Im a B object
I bounce!
void showContensofArray(void *data[], int len)
{
int i;
for(i=0;i<len;i++){
((Base*)(data[i]))->print();
}
}
And every Class should have an implementation of the method print() that knows how to print its values.
You could also use inheritance.
EDIT:
#Ricibob's answer is correct, but if you need to do the casting inside the function, you need to do something like this:
#include <iostream>
using namespace std;
class Base{
public:
virtual void print()=0;
};
class A: public Base{
public:
void print(){
cout<<"Object A"<<endl;
}
};
class B: public Base{
public:
void print(){
cout<<"Object B"<<endl;
}
};
void showContensofArray(void* data[], int len)
{
int i;
for(i=0;i<len;i++){
((Base*)(data[i]))->print();
}
}
int main(){
A a;
B b;
void* v[2];
v[0]= &a;
v[1] = &b;
showContensofArray(v,2);
return 0;
}
You can't evade inheritance.
Just cast back to the original type:
A* p1 = static_cast<A*>(data[0]);
B* p2 = static_cast<B*>(data[1]);

public, protected, private

Take a look at this code:
#include <iostream>
using namespace std;
class A
{
private:
int privatefield;
protected:
int protectedfield;
public:
int publicfield;
};
class B: private A
{
private:
A a;
public:
void test()
{
cout << this->publicfield << this->protectedfield << endl;
}
void test2()
{
cout << a.publicfield << a.protectedfield << endl;
}
};
int main()
{
B b;
b.test();
b.test2();
return 0;
}
B has access to this->protectedfield but hasn't to a.protectedfield. Why? Yet B is subclass of A.
B has access only to the protected fields in itself or other objects of type B (or possibly derived from B, if it sees them as B-s).
B does not have access to the protected fields of any other unrelated objects in the same inheritance tree.
An Apple has no right to access the internals of an Orange, even if they are both Fruits.
class Fruit
{
protected: int sweetness;
};
class Apple: public Fruit
{
public: Apple() { this->sweetness = 100; }
};
class Orange: public Fruit
{
public:
void evil_function(Fruit& f)
{
f.sweetness = -100; //doesn't compile!!
}
};
int main()
{
Apple apple;
Orange orange;
orange.evil_function(apple);
}
this->protectedfield:
B enherits of A, this means protectedfield is a property of itself now, so it is able to access it.
a.protectedfield:
a is a member of class B, this member has the protectedfield variable which is protected. B cannot touch it, because protected means only access from A within.
Lets break the whole code in small parts.Copy and paste this two code and try to compile!!!!
#include <iostream>
using namespace std;
class A
{
private:
int privatefield;
protected:
int protectedfield;
public:
int publicfield;
};
int main()
{
A a;
cout<<a.publicfield;
cout<<a.privatefield;/////not possible ! private data can not be seen by an object of that class
cout<<a.protectedfield;////again not possible. protected data is like privete data except it can be inherited by another.If inherited as private then they are private,if as protected then protected and if as public then also protected.
}
Now B inherits class A as private
#include <iostream>
using namespace std;
class A
{
private:
int privatefield;
protected:
int protectedfield;
public:
int publicfield;
};
class B: private A
{
private:
A a;
public:
void test()
{
cout << this->publicfield << this->protectedfield << endl;
}
void test2()
{
cout << a.publicfield << endl;
}
};
int main()
{
/*Now B will have both public and protected data as private!!!!
That means
B now looks like this class
Class B
{
private:
int protectedfield;
int publicfield;
}
As we have discussed private/protected data can not be accessed by object of the class
so you you can not do things like this
B b;
b.protectedfield; or b.publicfield;
*/
B b;
b.privatefield;////Error !!!
b.protectedfield/////error!!!!
}
Thanks!