Sponsored Content
Top Forums Programming Base class's variables not accessible???? Post 302588585 by gabam on Monday 9th of January 2012 11:27:16 AM
Old 01-09-2012
Base class's variables not accessible????

Hi friends,
The derived class cannot access the base class's variables in my program. You can have a look at my code, I am actually using class templates.



Code:
 
#include <iostream>
using namespace std;
template <class T>
class Sum
{
    friend void Check(Sum &s, T a, T b)
{
    T x, y;
    x = (a > 0 && a <= 10000) ? a : 0;
    y = (b > 0 && b <= 10000) ? b : 0;
    s.Write(x, y);
}
  public:
  Sum();
  void Read();
  private:
  T sum;
  void Write(T, T);
  T Total(T, T);
  protected:
T x;
T y;
T z;
};
template <class T>
Sum<T>::Sum()
{
    x = 0;
    y = 0;
}
template <class T>
void Sum<T>::Read()
{
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    cout << "Sum = " << Total(x, y) << endl << endl;
}
template <class T>
void Sum<T>::Write(T a, T b)
{
    x = a;
    y = b;
}
template <class T>
T Sum<T>::Total(T a, T b)
{
    sum = a + b;
    return sum;
}
template <class T>
class GrandSum : public Sum<T>
{
    public:
    GrandSum();
    T GrandTotal(T, T, T, T, T, T);
    void Show();
    private:
    T Gx;
    T Gy;
    T Gz;
    T Gsum;
};
template <class T>
GrandSum<T>::GrandSum()
{
    Gx = 0;
    Gy = 0;
    Gz = 0;
    z = 3;
    Gsum = 0;
}
template <class T>
T GrandSum<T>::GrandTotal(T a, T b, T c, T d, T e, T f)
{
x = a;
y = b;
z = c;
    Gx = d;
    Gy = e;
    Gz = f;
    Gsum = x + y + z + Gx + Gy + Gz;
    return Gsum;
}
template <class T>
void GrandSum<T>::Show()
{
    cout << Gsum;
}
int main()
{
    Sum<int> s1;
    GrandSum<int> s2;
    int tot;
    Check(s1, 1, 2);
    tot = s2.GrandTotal(1, 2, 3, 4, 5, 6);
    cout << tot << endl;
    return 0;
}

The red variables cannot be accessed by the derived class, the compiler gives error highlighting the variable names in blue.

I am using the same program without using templates, and it works fine, there must be something wront with the templates I guess. Here have a look at the version of the same program without templates




Code:
 
#include <iostream>
using namespace std;
class Sum
{
friend void Check(Sum &s, int a, int b)
{
int x, y;
x = (a > 0 && a <= 10000) ? a : 0;
y = (b > 0 && b <= 10000) ? b : 0;
s.Write(x, y);
}
public:
Sum();
void Read();
private:
int sum;
void Write(int, int);
int Total(int, int);
protected:
int x;
int y;
int z;
};
 
Sum::Sum()
{
x = 0;
y = 0;
}
void Sum::Read()
{
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "Sum = " << Total(x, y) << endl << endl;
}
void Sum::Write(int a, int b)
{
x = a;
y = b;
}
int Sum::Total(int a, int b)
{
sum = a + b;
return sum;
}
 
class GrandSum : public Sum
{
public:
GrandSum();
int GrandTotal(int, int, int, int, int, int);
void Show();
private:
int Gx;
int Gy;
int Gz;
int Gsum;
};
 
GrandSum::GrandSum()
{
Gx = 0;
Gy = 0;
Gz = 0;
z = 3;
Gsum = 0;
}
 
int GrandSum::GrandTotal(int a, int b, int c, int d, int e, int f)
{
x = a;
y = b;
z = c;
Gx = d;
Gy = e;
Gz = f;
Gsum = x + y + z + Gx + Gy + Gz;
return Gsum;
}
 
void GrandSum::Show()
{
cout << Gsum;
}
int main()
{
Sum s1;
GrandSum s2;
int tot;
Check(s1, 1, 2);
tot = s2.GrandTotal(1, 2, 3, 4, 5, 6);
cout << tot << endl;
return 0;
}

Could you please tell me where I am going wrong with this program.
Waiting for your wonderful replies!
Thanks alot in advance!
 

9 More Discussions You Might Find Interesting

1. Programming

How to make a function friend to both base and derived class

Hi, I have a base class and derived a class from the base class, i want to print & read the data for the object created for the derived class,so i have overloaded both the << and >> operators and also have done the foward declaration. Below is the code snippet, #include <iostream> class... (3 Replies)
Discussion started by: ennstate
3 Replies

2. UNIX for Dummies Questions & Answers

car class (not school class)

im just trying to have some fun and kill some time writing a c++ program that has a person type in a car make and model then gives them a year and a price. or something like that. i always have problems getting it goin but once the ball is rolling im usually pretty good. anyone wanna help me out? ... (1 Reply)
Discussion started by: rickym2626
1 Replies

3. Programming

C++ class definition with a member of the same class

Hi, i have a question about C++. Is it possible to declare a class with a member ot the same class? For example, a linked list or i want to convert this C code to C++ class (Elemento) typedef struct elemento { char name; char value; List<struct elemento> ltElementos; ... (7 Replies)
Discussion started by: pogdorica
7 Replies

4. UNIX for Dummies Questions & Answers

NameServer not accessible

Hi everybody I am facing a little problem with names servers. I have a VPS with Kloxo installed on it I have registered 2 name servers (i.e. ns1.domain.com and ns2.domain.com) past one month. These nameservers are not accessible yet. I have check the nameserver on internic site and they are... (3 Replies)
Discussion started by: rizwan65
3 Replies

5. Red Hat

CentOS 6.1 base install (like FreeBSD base install)?

Hello, What is the simplest way to install CentOS 6.1 with console base-system only using official LiveDVD image on VirtualBox machine? I'd like to get simplest console with network support like FreeBSD base installation. Then, install services which I need. The installer jest extracts the... (2 Replies)
Discussion started by: newbie_develope
2 Replies

6. Programming

static use for class inside the same class c++

Hi, I believe the next code is wrong: class Egg { Egg e; int i; Egg(int ii=0) : i(ii) {} }; because you would end up with an endless definition (memory allocation) of Egg objects, thus int i. Ok, so God Eckel proposes for a singleton: class Egg { static Egg e; int... (5 Replies)
Discussion started by: xavipoes
5 Replies

7. UNIX for Advanced & Expert Users

Get pointer for existing device class (struct class) in Linux kernel module

Hi all! I am trying to register a device in an existing device class, but I am having trouble getting the pointer to an existing class. I can create a class in a module, get the pointer to it and then use it to register the device with: *cl = class_create(THIS_MODULE, className);... (0 Replies)
Discussion started by: hdaniel@ualg.pt
0 Replies

8. Programming

Size of Derived class, upon virtual base class inheritance

I have the two class definition as follows. class A { public: int a; }; class B : virtual public A{ }; The size of class A is shown as 4, and size of class B is shown as 16. Why is this effect ?. (2 Replies)
Discussion started by: techmonk
2 Replies

9. Programming

C++ : Base class member function not accessible from derived class

Hello All, I am a learner in C++. I was testing my inheritance knowledge with following piece of code. #include <iostream> using namespace std; class base { public : void display() { cout << "In base display()" << endl; } void display(int k) {... (2 Replies)
Discussion started by: anand.shah
2 Replies
All times are GMT -4. The time now is 05:08 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy