c++

Hi to all,can u tell me,suppose i have a class in private section i declar a int p,then i derived another class there also i have a private data int p with same name so how can access that data as both in base and derived clas both have same name,,,,,,how can i differentita two data
First thing to do is change one of the names. If you can't do that then...

1
2
3
4
5
6
7
8
9
10
11
12
void derived_class::my_func(void)
{
   base_class::p = 1; // OK - references base class variable
   p = 2;  // OK - references derived_class variable by default or ...
   derived_class::p = 2; // OK - explicitly references derived_class variable
}

void base_class::my_func2(void)
{
   derived_class::p = 1; // Not allowed to reference derived members from base class
   p = 1; // OK - references base_class variable only
}


Hope this helps
Last edited on
The answer is don't do that. (It won't work anyway.)

The purpose of private sections is to keep others out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class parent
  {
  private:
    // this is my stuff. everyone else: access denied.

  protected:
    // this is stuff I'm willing to share with the family,
    // but not strangers.

  public:
    // this is what everyone can see of me.
  };

class child: public parent
  {
  private:
    // my private stuff. I don't know what Dad's private stuff is.

  protected:
    // family stuff I got from Dad.

  public:
    // etc.
  };

I think it is worth your time to spend a bit over at the C++FAQ-Lite and read through the sections on inheritance
http://www.parashift.com/c++-faq-lite/

Hope this helps.
Respected experts please chek my simple code and help me that in this code the division and modulus are not comming correct.

#include <iostream.h>

main()
{
//declaring variables//
int value1 , value2 , add , subtract , multiply , module , average , square;
float divide;

//asking the user for values //
cout << " Please enter the two numbers" << endl << endl;

// Getting values from the user//
cout << " Number one please" << endl << endl;
cin >> value1;
cout << " Number two please " << endl << endl;
cin >> value2;

//starting operations//

divide = value1 / value2;
add = value1 + value2;
subtract = value1 - value2;
multiply = value1 * value2;
module = value1 % value2;
square = value1 * value1;
average = value1 + value2 / 2;

//printing output for Addition//
cout << "The addition of " <<value1 << "and" <<value2 << " is" << endl << add <<endl;

//printing output for Subtraction//
cout << "The Subtraction of " <<value1 << "and" <<value2 << " is" << endl << subtract <<endl;

//printing output for Multiplication//
cout << "The Multiplication of " <<value1 << "and" <<value2 << " is" << endl << multiply <<endl;

//printing output for Division//
cout << "The Division of " <<value1 << "and" <<value2 << " is" << endl << divide <<endl;

//printing output for Modulus//
cout << "The Modulus of " <<value1 << "and" <<value2 << " is" << endl << module <<endl;

//printing output for Square//
cout << "The Square of " <<value1 << "and" <<value2 << " is" << endl << square <<endl;

//printing output for Average//
cout << "The Average of " <<value1 << "and" <<value2 << " is" << endl << average <<endl;



}
Topic archived. No new replies allowed.