C++ Inheritance problem??????


 
Thread Tools Search this Thread
Top Forums Programming C++ Inheritance problem??????
# 1  
Old 01-08-2012
C++ Inheritance problem??????

Hi friends,
I hope u people are ok and doing fine.
I have this small problem with the derived class. I have created te base class and there is a small problem with the definition of the derived class which the compiler is pointing out, could you please help me. Here is my code


Code:
 
#ifndef SUM_H_INCLUDED
#define SUM_H_INCLUDED
#include <iostream>
using namespace std;
template <class T>
class Sum
{
friend void Check(Sum &s, T a, T b)     // Friend Function
{
    T x, y;
    x = (a > 0 && a <= 10000) ? a : 0;
    y = (b > 0 && b <= 10000) ? b : 0;
    s.Write(x, y);
}
private:
T x;
T y;
void Write(T a, T b)
{
    x = a;
    y = b;
}
T Add(T a, T b)          // Utility Function
{
    return a + b;
}
public:
Sum()
{
    x = 0;
    y = 0;
}
void Read()
{
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    cout << "Sum = " << Add(x, y) << endl << endl;
}
protected:
T z;
};



Code:
 
#include <iostream>
#include "Sum.h"
using namespace std;
template <class T>
class GrandSum : public Sum     // Derived class of base class Sum
{
private:
T X;
T Y;
public:
GrandSum()
{
X = 0;
Y = 0;
//z = 0;
}
void Get(T a, T b, T c)
{
    X = a;
    Y = b;
//    z = c;
}
void Show()
{
    T sum;
    sum = /*x + y + z*/ + X + Y;
    cout << sum << endl;
}
};
int main()
{
    GrandSum<int> s2;
    Sum<int> s1;
    Check(s1, 1, 2);
    s1.Read();
    return 0;
}

The compiler only gives one error, and that is in the red part of the code, I don't know what is wrong there.
I am using CodeBlocks, with MinGW compiler, and it says

Code:
 
C:\Users\dawood\CodeBlocks\PL2_Final\main.cpp|6|error: expected class-name before '{' token
||=== Build finished: 1 errors, 0 warnings ===

# 2  
Old 01-08-2012
I think it needs to be Sum<T>, not Sum, because Sum is also a template
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-09-2012
Quote:
Originally Posted by Corona688
I think it needs to be Sum<T>, not Sum, because Sum is also a template
Thanks alot, it worked!
This User Gave Thanks to gabam For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

What is wrong with below python inheritance code?

I am using python 3.4. Below is the exception I am getting- Traceback (most recent call last): File "./oop.py", line 20, in <module> y = DerivedClass("Manu") File "./oop.py", line 15, in __init__ super().__init__(self,value) TypeError: __init__() takes 2 positional arguments but... (2 Replies)
Discussion started by: Tanu
2 Replies

2. Red Hat

Ulimit Inheritance

Hi , If i start mysqld or httpd as root user which inturn starts them as "mysql" or "apache" user, will the ulimit of "root" user or ulimit of "mysql/apache" user be set for the mysql/apache processes. My understanding is that the ulimit of the user who initiates the process(root in this... (2 Replies)
Discussion started by: Hari_Ganesh
2 Replies

3. Programming

Difference in multiple inheritance and multilevel inheritance: same method name ambiguity problem

Hi, In multi-level inheritance: class A { public: void fun() { cout << "A" << endl; } }; class B : public A { public: void fun() { cout << "A" << endl; } }; class C : public B { }; int main() { C c; c.fun(); // Ans: A } (1 Reply)
Discussion started by: royalibrahim
1 Replies

4. Programming

Inheritance

whats the use of inheriting with access specifier as private..? Please specify the answer with a simple example... Regards -- Madhu (5 Replies)
Discussion started by: MadhuM
5 Replies

5. Shell Programming and Scripting

Inheritance in Perl

package Inventory_item; sub new { my($class) = shift; bless { "PART_NUM" => undef, "QTY_ON_HAND" => undef }, $class; } package main; $item = Inventory_item->new(); Can any one please help me in brief explaining the line... (5 Replies)
Discussion started by: parthmittal2007
5 Replies

6. AIX

WLM inheritance

Is none of you using WLM? A search gives no matches.... Anyway, I have set up a class with inheritance = yes. In the rules characteristics I have chosen a shell script as an application. This script is caught by the class, but not the child processes, which have the PID of the script as the... (6 Replies)
Discussion started by: firefox111
6 Replies

7. UNIX for Dummies Questions & Answers

Question on variable inheritance & code statements

1) I have the below code in concattxnrecords.sh shell script and it is calling the genericVars.sh shell script which is mentioned as below has some code inside it which would intialize some variables in it, now my question is will this shell script would inherit those variable definitions or not... (3 Replies)
Discussion started by: Ariean
3 Replies

8. Shell Programming and Scripting

Makefile: Parent - Child Inheritance and export

Hi, I have a number of Makefiles, including a couple of files that I include in Makefiles, a few scripts that are executed through Makefiles, and I have problems with environment variables that are not inherited to the scripts properly. Simplified scenario: rootdir/Makefile: all: ... (1 Reply)
Discussion started by: Shompis
1 Replies

9. Shell Programming and Scripting

perl - variable inheritance

Hey Everyone, Does anyone know how - or if it's even possible - for a child perl script to inherit the variables of a parent perl script? In a shell script, you would use "export" for example. I am running Perl 5.8. Basically, let's say "perl1.pl" calls "perl2.pl" and I want "perl2.pl" to... (2 Replies)
Discussion started by: gsatch
2 Replies
Login or Register to Ask a Question