class inheritence with constructor parameters

This is a discussion on class inheritence with constructor parameters within the C++ Programming forums, part of the General Programming Boards category; okay all I'm a little rusty on class inheritence here so if you could all refresh my memory please I ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    class inheritence with constructor parameters

    okay all I'm a little rusty on class inheritence here so if you could all refresh my memory please I have the following classes here
    the constructor recieves two values an integer and a double so if that is the case how do you build the constructor for the derived class.
    here is main
    Code:
    #include<iostream>
    #include <iostream>
    #include "Score.h"
    
    
    void main()
    {
    	numbers p(1, 0);
    	p.createlist();
    	cout<<"TRY THIS ON";
    }
    here is the header
    Code:
    #ifndef Score
    #define Score
    #include <fstream>
    
    using namespace std;
    
    // Number *num = new Number(5.3);
    class numbers
    {
    public:
    	fstream f;
    	struct node
    	{
    		double value;
    		int type;
    		node *next;
    		node *back;
    	};
    
    numbers(int category,  double value )
    {	int m=category;      //this is your category;
    	double  n=value;//this is your value;
    }
    void createlist()
    {
    	node*	list=NULL;
    	node *p, *r, *q;
    	int m;
    	double n;
    	f.open("f.txt", ios::in);
    	while(!f.eof())
    	{
    	if(list==NULL)
    	{
    		f.seekp(0, ios::beg);
    		f>>m;
    		f.seekp(1, ios::cur);
    		f>>n;
    		p=new(node);
    		p->type=m;
    		p->value=n;
    		list=p;
    		p->next=NULL;
    		p->back=list;
    	
    	}
    	else if(list==p) 
    	{
    		f.seekp(1, ios::cur);
    		f>>m;
    		f.seekp(1, ios::cur);
    		r=new(node);
    		f>>n;
    		p->next=r;
    		r->type=m;
    		r->value=n;
    		r->back=p;
    		r->next=NULL;
    		p=p->next;
    	}
    	else 
    	{
    		f.seekp(1, ios::cur);
    		f>>m;
    		f.seekp(1, ios::cur);
    		f>>n;
    		r=new(node);
    		r->back=p;
    		r->next=NULL;
    		p->next=r;
    		r->value=n;
    		r->type=m;
    		p=p->next;
    	}
    
    	}
    }
    
    };
    class Score : public numbers
    {
    public:
    Score(int a, double b):numbers(int category, double value)
    	{ int a=category;
    	  double val=value;
    	}
    };
    
    #endif
    now I'm a bit rusty so I kind of forgot the correct syntax for linking the constructor for the derived class to the Base class
    any advice is welcome here

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    No need to re-initialize the member variables in the derived constructor, just call the base constructor in the initialization list with the appropriate values:
    Code:
    Score(int a, double b) : numbers(a, b) {
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    BTW, there are a few other problems with your posted code - You should use something else for your header guard (like #ifndef SCORE_H instead of #ifndef Score) because you are defining Score to be nothing and every where that token is found in the file it will be replaced. Also your method for checking for eof() will usually not work as you expect, it will probably run through the loop an extra time and add the last value twice to your list.

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    OK lets take this baby apart

    Code:
    numbers(int category,  double value )
    {	int m=category;      //this is your category;
    	double  n=value;//this is your value;
    }
    OK first, m and n are going to be local variable since you have defined them within the contructor and thus will not be available after the contructor has finished,

    ...edit...snip ... misread program

    Better would be to create member variables that you can assign category and value to. That way they are avalable to all member functions.

    You can initiate the base class from the derived like so:
    Code:
    Score(int a, double b):numbers(a, b)
    	{ 
    	}
    So basically you are using the parameters you received from Score, to call the contructor numbers.

    You could do something similar to what you tried and initialize the member variables seperately, but this way is the simplist. For example:

    Code:
    Score(int a, double b):numbers::m_category(a), numbers::m_value(b)
    	{
    
    	}
    // or
    
    Score(int a, double b)
    	{
    		m_category =a;
    		m_value = b;
    
    	}
    note however, the m_ in front of the variables indicating these are member variables and NOT the names of the parameter of the constructors
    Last edited by Darryl; 06-20-2005 at 06:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  4. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 10:33 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21