how to call the base func in the derived class!

how do we call the functions of the base class in
the derived class?

is there any presedure shulde i use?!
Hello ARWA,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class A
{
protected:
  void Foo();
};

class B : public A
{
  void Bar()
  {
    Foo();
  }
  void Foo() // note: the same name as in A (which hides Foo() in A)
  {
    A::Foo();
  }
};
thanks this is very clear,but
i have some query can i explisitly call Foo function in the derived class
exp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class A
{
protected:
  void Foo();
};

class B : public A
{
  void Bar()
  {
    
    A::Foo();
  }
};
yes, you can
ok,in my code iv saperated the implimentation of the class in other cpp
but it says that the "pointType " is NOT a class or a namespace name

this is part of the code:

1
2
3
4
5
6
void circle::printR()
 {
cout <<"the center of the circle is ["<<pointType::printXY<<","<<poinType::getY<<"]"<<endl;
	cout<<"witch has an area : "<<fixed<<showpoint<<setprecision(2)<<area()<<endl;
	 cout <<"and circumference : "<<circumference()<<endl;
 }


what should be the mistake?!
what's the relation of circle and pointType? Is pointType the base class of circle like ('A' to 'B')?
if 'printXY' is a function you need to write at least 'printXY()'
yes,pointType is the base of the circle ..
i have updated the code and did 'print()
but no use.....even the setprecision is not recognized!! and i included the cmath.
the thing is that in the question they specify the number of questions i cant do the implemntation
of the code up.
without seeing the definition of the types in question I'm sorry but I can't tell what's wrong.

for setprecision you need to include <iomanip> not cmath
this is the circle class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# include "pointType.h"
# ifndef circle_H
# define circle_H


class circle : public pointType
{
public:
	//constructor
	circle(double r=0.0,int x=0,int y=0);
	void setRadius(double);
	double getRadius()const;
	double area()const;
	double circumference()const;
	void printR();

protected:
	double radius;
}
#endif  



this is circle imp

1
2
3
4
5
 # include "circle.h"
# include <iostream>
# include "pointType.h"
# include <cmath>
 using namespace std;





this is point imp

1
2
3
4
 # include "pointType.h"
# include <iostream>
using namespace std;
 




this is the client

1
2
3
4
5
6
7
8
9
 # include <iostream>
# include "circle.h"
# include "pointType.h"

using namespace std;

int main()
{
...return 0}


Last edited on
ok, but 'pointType' is what matters.
this is the decleration of pointType:

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

# ifndef pointType_H
# define pointType_H
class pointType{
protected:
	int x,y;
public:
	// constructor
	pointType(int x=0,int y=0 );
	//mutator function
	void setXY(int,int);
	//accessor function
	int getX()const;
	int getY()const;
	void printXY();



};

# endif 

actualy the circle is apoint or the circle has apoint!
so there are some functions i want to use in the circle implementation...
i hope its clear
from what you posted I don't see any reason fo that
"pointType " is NOT a class or a namespace name

Maybe there's a missing include or you've defined 'pointType' otherwise?

but this line in plain wrong:
cout <<"the center of the circle is ["<<pointType::printXY<<","<<poinType::getY<<"]"<<endl;
this is certainly what you want
cout <<"the center of the circle is ["<<getX()<<","<<getY()<<"]"<<endl; never forget '()'!
printXY() returns nothing so it can't be used in that context

i said that before you need to write parentheses in order to call a function in C/C++. Maybe you learned Pascal or Delphi?

You can call any function from a base class without (base_class::...) except you hide that base class function within your class.

Topic archived. No new replies allowed.