473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

ask a small question!!

can someone explain to me?i feel puzzled
1, CBase * pba = new CDerived;
2, CBase * pbb = new CBase;

CBase is Base class and CDerived is Derived class,but what is the
difference between 1 and 2?

thanks a lot!

Dec 8 '06 #1
7 1347

fo******@gmail.com wrote:
can someone explain to me?i feel puzzled
1, CBase * pba = new CDerived;
2, CBase * pbb = new CBase;

CBase is Base class and CDerived is Derived class,but what is the
difference between 1 and 2?

thanks a lot!
the differences can be found on many books of C++
I don't remember, but I use both based on what each can and can't do

You sure can write a progrm to test, something simple as follows

class base{
public:
void show(){cout<<"bbbbbb\n";};
};

class dbase:public base{
public:
void show(){cout<<"dddbbb\n";}
};

int main(){
base* pb=new base();
base*db=new dbase();
pb->show();
db->show();
delete pb;
delete db;
return 0;
}

there are of course many other points depending upon construction of
functions, polymorphism, including speed increase, interator concepts,
type relations in inheritance hierarchy in which such base pointers
might become useful with their own meanings etc

Dec 8 '06 #2
On Dec 8, 11:28 am, foodh...@gmail.com wrote:
can someone explain to me?i feel puzzled
1, CBase * pba = new CDerived;
2, CBase * pbb = new CBase;

CBase is Base class and CDerived is Derived class,but what is the
difference between 1 and 2?
The differance is that 1 creates a new CDerived object and 2 created a
new CBase object. This can be very useful together with virtual
functions:

#include <iostream>

struct base {
virtual void print();
}

struct derived : public base {
virtual void print();
}

void base::print() {
std::cout << "base\n";
}

void derived::print() {
std::cout << "derived\n";
}

int main() {
base* b = new base();
base* d = new derived();

b->print();
d->print();
}

Output:
base
derived

So, by using virtual funtions and base-pointers (works with references
too) we can use a derived object just like a base object (for example
pass it to functions like void foo(base*) ) but it will still act like
a derived object. So you can modify the behaviour of a function in the
derived class but still use it in all the places where a base could be
used. This is polymorphism.

--
Erik Wikstörm

Dec 8 '06 #3
Douglas Dude wrote:
>
there are of course many other points depending upon construction of
functions, polymorphism, including speed increase, interator concepts,
type relations in inheritance hierarchy in which such base pointers
might become useful with their own meanings etc
They might also do well to poitn out your example program causes
undefined behavior as the base class does not have a virtual
destructor and you destroy the derived object through the base
pointer.
Dec 8 '06 #4

er****@student.chalmers.se wrote:
On Dec 8, 11:28 am, foodh...@gmail.com wrote:
can someone explain to me?i feel puzzled
1, CBase * pba = new CDerived;
2, CBase * pbb = new CBase;

CBase is Base class and CDerived is Derived class,but what is the
difference between 1 and 2?

The differance is that 1 creates a new CDerived object and 2 created a
new CBase object. This can be very useful together with virtual
functions:

#include <iostream>

struct base {
virtual void print();
}

struct derived : public base {
virtual void print();
}

void base::print() {
std::cout << "base\n";
}

void derived::print() {
std::cout << "derived\n";
}

int main() {
base* b = new base();
base* d = new derived();

b->print();
d->print();
}

Output:
base
derived

So, by using virtual funtions and base-pointers (works with references
too) we can use a derived object just like a base object (for example
pass it to functions like void foo(base*) ) but it will still act like
a derived object. So you can modify the behaviour of a function in the
derived class but still use it in all the places where a base could be
used. This is polymorphism.

--
Erik Wikstörm
thanks i think i understand a little,you mean even we define like that:
derived* d=new derived()
but we can still use dynamic_cast<base*>d
so we can make the same result??
is it right?

Dec 9 '06 #5
fo******@hotmail.com wrote:
er****@student.chalmers.se wrote:
>On Dec 8, 11:28 am, foodh...@gmail.com wrote:
>> can someone explain to me?i feel puzzled
1, CBase * pba = new CDerived;
2, CBase * pbb = new CBase;

CBase is Base class and CDerived is Derived class,but what is the
difference between 1 and 2?
The differance is that 1 creates a new CDerived object and 2 created a
new CBase object. This can be very useful together with virtual
functions:

#include <iostream>

struct base {
virtual void print();
}

struct derived : public base {
virtual void print();
}

void base::print() {
std::cout << "base\n";
}

void derived::print() {
std::cout << "derived\n";
}

int main() {
base* b = new base();
base* d = new derived();

b->print();
d->print();
}

Output:
base
derived

So, by using virtual funtions and base-pointers (works with references
too) we can use a derived object just like a base object (for example
pass it to functions like void foo(base*) ) but it will still act like
a derived object. So you can modify the behaviour of a function in the
derived class but still use it in all the places where a base could be
used. This is polymorphism.

--
Erik Wikstörm

thanks i think i understand a little,you mean even we define like that:
derived* d=new derived()
but we can still use dynamic_cast<base*>d
so we can make the same result??
is it right?
There's an implicit cast from derived to base. Going from a derived
class to a base class is called an "upcast". You don't need a cast
operator for upcasting. Going the other way (base to derived) is a
downcast. You need either static_cast or (better) reinterpret_cast to
downcast.

class base
{
public:
virtual ~base() { }
};

class derived : public base
{
};

derived *pd = new derived;
base *pb = pd;
pd = dynamic_cast<derived*>(pb);
Dec 9 '06 #6
On Dec 8, 11:28 am, foodh...@gmail.com wrote:
can someone explain to me?i feel puzzled
1, CBase * pba = new CDerived;
2, CBase * pbb = new CBase;
>
CBase is Base class and CDerived is Derived class,but what is the
difference between 1 and 2?
thanks i think i understand a little,you mean even we define like that:
derived* d=new derived()
but we can still use dynamic_cast<base*>d
so we can make the same result??
is it right?
Ok. The main point is that you can have pointers to base class objects
and call a virtual function on the pointers and at run-time the
function for either the base or derived class will be called depending
on if the pointer actually points to a base or derived object. This is
called polymorphism.

As far as dynamic_cast, the purpose is to convert a base pointer (or
reference) to a derived pointer and at runtime to check that your
pointer really does point to a derived object. If it doesn't then the
conversion will return a 0 pointer (or throw exception for reference)
and prevent you from trying to call a derived object function on an
object that really is not a derived object.

---
Ivan
http://www.0x4849.net

Dec 9 '06 #7
On 2006-12-09 05:13, red floyd wrote:
There's an implicit cast from derived to base. Going from a derived
class to a base class is called an "upcast". You don't need a cast
operator for upcasting. Going the other way (base to derived) is a
downcast. You need either static_cast or (better) reinterpret_cast to
downcast.
No, reinterpret_cast should not be used if the types you are converting
between are related, for those things you have static_cast and
dynamic_cast, but not reinterpret_cast.

--
Erik Wikström
Dec 9 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: VIJAY KUMAR | last post by:
I know RFV works(executes) on both Client side and server side. But if I want to execute only on Client Side due to performance reason then what I have to do? Ex: I have a Text box which must...
2
by: trabajar | last post by:
I have a piece of software - it compiles fine using nmake (started from cygwin, using visual c++). The output of nmake explorernodequery.dll is:...
7
by: Sharon | last post by:
Hiya I have a small question, I saw this piece of code somewhere (it's for creating a customized context menu) and I was wondering: Why is it that the STYLE and SCRIPT-tags are broken up into...
10
by: volunteers | last post by:
Hi, group, I am using a for loop and print such like: for( i=0; i<10; i++) printf("%d", i); so the result look like '1234...', what I want is all the numbers are in one position, mean the...
1
by: Gena | last post by:
Hi , I'm a newbe to programming and have a small question: I made a small program with a class. in the class's header file I have : double *ptr_output; Void main() { double result;
2
by: BKMiller | last post by:
Hello everyone, I'm just getting started playing around with C++, so please don't laugh too loudly at my crude source code, okay? (o^^o) I'm combining together several introductory exercises...
5
by: ina | last post by:
Hello guys, My name is ina and I have a problem with a file xlst. I am newbie and sorry for this question, probably must be very simple. I have this xml file <?xml version="1.0"?>...
16
by: Bruce W. Darby | last post by:
I've almost completed my little application for work. This weekend I've been working on Streams so I can write a logfile showing the work that was accomplished. Wanting to make each logfile...
18
by: SpiralCorp | last post by:
int divide (int a, int b) { int r; r=a/b; return (r); } int main () { int result = divide (20,4); cout << result
2
by: shror | last post by:
I need help in a small problem, I have created a registration form and I want my users not leave the username empty or even enter any number of spaces because sometimes I found that the users...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.