473,397 Members | 2,068 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,397 software developers and data experts.

Need help from you guys about classes

Hi guys,
I have question about classes.
when u create class called Test.
and you define variable Test c;
so does this act like( a variable c of type Test pointing to an abject )?
The thing that I am confused with java where you say Test c = new Test; whre
you can move this Test object around by saying "Test b" and then b = c;(so c
and b points to the same object).
while in c++ I think it has different meaning(like b would have different
object cloned from c)?
so would explain me the difference
I hope that you understood what I ment in this confusing question.
Thanx alot for ytour efforts guys. I really apprecuate any help.
Regards,
Jul 22 '05 #1
7 1563
On Tue, 20 Jan 2004 01:30:59 GMT, "Snake" <ha***@rogers.com> wrote:
I have question about classes.
when u create class called Test.
and you define variable Test c;
so does this act like( a variable c of type Test pointing to an abject )?
No, it's a variable c that directly is a Test object.

The thing that I am confused with java where you say Test c = new Test; whre
you can move this Test object around by saying "Test b" and then b = c;(so c
and b points to the same object).
while in c++ I think it has different meaning(like b would have different
object cloned from c)?
Right, although in C++ "clone" usually implies creating a copy in
dynamically allocated memory.
so would explain me the difference


In Java variables of class types contain references to objects.

In C++ they contain objects directly.

C++ does not have a direct analog of Java references, but you can
use
A) Pointers.
Provides assignment like Java references, as well as more low-level
stuff like pointer arithmetic not present in Java.

B) References.
Provides transparent notation like Java references do (when c is a C++
reference the expression c refers directly to the object referred to by
c), but you cannot change what a reference refers to.

C) Smart-pointers.
E.g. std::auto_ptr or boost::shared_ptr. Provides automatic memory
management sort of like Java references (the main difference is that
most C++ smart-pointers use reference counting, which doesn't handle
circular data structures automatically), and typically also reference
assignment, but not transparent notation.
(C) is not a built-in feature of C++, it's just a way of using the language
mechanisms, such as template classes, to implement automatic memory management.

Jul 22 '05 #2
"Snake" <ha***@rogers.com> wrote...
I have question about classes.
when u create class called Test.
and you define variable Test c;
so does this act like( a variable c of type Test pointing to an abject )?
Rather, an identifier 'c' that designates an object of type Test.
The thing that I am confused with java where you say Test c = new Test; whre you can move this Test object around by saying "Test b" and then b = c;(so c and b points to the same object).
while in c++ I think it has different meaning(like b would have different
object cloned from c)?
Right.
so would explain me the difference


The difference is that in C++ there are three ways to create an object:
static, automatic, and dynamic. In Java... Well, you'll have to ask
in a Java newsgroup.

Static objects are either created outside of any function, or explicitly
declared "static". Automatic objects are created inside functions. And
dynamic objects are created in free store with the help of 'new' operator.

Example

class Test {}; // class Test defined

Test st; // static object

int main()
{
static Test stt; // another static object
Test at; // automatic object
Test *pdt = new Test; // a pointer to a dynamic object

delete pdt; // have to delete what you create dynamically
return 0;
}

Victor
Jul 22 '05 #3
Thanks alot guys for the help.That really clarified alot of thing.
But one more that I am not sure I really got.
In the example I mentiond(b = c).
do b and c each has spearated " identical" objects no matter how many
attributes are there?
ok,what if the object Test has a pointer d that refers to another different
object.
will b and c have two different pointers refering to the same object
referred by d previously or two pointers refereeing to two different object?
a bit confusing,eh!!
Thanx again, I really need help over this.
Regards

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:_L%Ob.87988$5V2.135777@attbi_s53...
"Snake" <ha***@rogers.com> wrote...
I have question about classes.
when u create class called Test.
and you define variable Test c;
so does this act like( a variable c of type Test pointing to an abject )?

Rather, an identifier 'c' that designates an object of type Test.
The thing that I am confused with java where you say Test c = new Test; whre
you can move this Test object around by saying "Test b" and then b =

c;(so c
and b points to the same object).
while in c++ I think it has different meaning(like b would have

different object cloned from c)?


Right.
so would explain me the difference


The difference is that in C++ there are three ways to create an object:
static, automatic, and dynamic. In Java... Well, you'll have to ask
in a Java newsgroup.

Static objects are either created outside of any function, or explicitly
declared "static". Automatic objects are created inside functions. And
dynamic objects are created in free store with the help of 'new' operator.

Example

class Test {}; // class Test defined

Test st; // static object

int main()
{
static Test stt; // another static object
Test at; // automatic object
Test *pdt = new Test; // a pointer to a dynamic object

delete pdt; // have to delete what you create dynamically
return 0;
}

Victor

Jul 22 '05 #4
"Snake" <ha***@rogers.com> wrote...
Thanks alot guys for the help.That really clarified alot of thing.
But one more that I am not sure I really got.
In the example I mentiond(b = c).
do b and c each has spearated " identical" objects no matter how many
attributes are there?
Not necessarily. For all it's worth, b and c can be associated with
objects of two completely different classes, with the exception that
the b's type needs to define the assignment operator that accepts
the right side convertible from the c's type.

Or perhaps I just didn't understand the question...
ok,what if the object Test has a pointer d that refers to another different object.
will b and c have two different pointers refering to the same object
referred by d previously or two pointers refereeing to two different

object?

Alright, so you say b and c are two different objects of the same type
that doesn't necessarily [re]define the assignment operator. And that
that type has a pointer as a member. Fine. During the assignment,
each member's assignment semantics will be followed (unless the operator=
is user-defined). For pointers that mean that after assignment is done,
they will point to the same object.

It can be circumvented, if you define your own assignment operator.

Example:

class Dummy {};
class NothingSpecial {
Dummy *p_;
public:
NothingSpecial(Dummy* p) : p_(p) {}
};

class OverloadedAssignment {
Dummy *p_;
public:
OverloadedAssignment(Dummy *p) : p_(p) {}
void operator =(OverloadedAssignment& oa) { } // p_ is left alone
};

int main() {
Dummy d1, d2;
NothingSpecial ns1(&d1), ns2(&d2); // ns1.p_ and ns2.p_ point to
// different Dummy objects
ns2 = ns1; // assignment semantics for pointers means now
// ns1.p_ and ns2.p_ point to the same object

OverloadedAssignment os1(&d1), os2(&d2); // point to different
os2 = os1; // overloaded assingment operator prevents copying
// of the p_ member -- they _STILL_ point to diff. obj
}

Victor
Jul 22 '05 #5

"Snake" <ha***@rogers.com> wrote in message
news:nx******************@news04.bloor.is.net.cabl e.rogers.com...
Hi guys,
I have question about classes.
when u create class called Test.
and you define variable Test c;
so does this act like( a variable c of type Test pointing to an abject )?
No, it's a variable of type Type. It IS the object - it doesn't POINT to
the object.
The thing that I am confused with java where you say Test c = new Test; whre you can move this Test object around by saying "Test b" and then b = c;(so c and b points to the same object).
That's different - you used new. In that case you have a pointer.
while in c++ I think it has different meaning(like b would have different
object cloned from c)?


If you used "new" in C++, then you'd have a pointer too.
Jul 22 '05 #6

"Snake" <ha***@rogers.com> wrote in message
news:XY********************@twister01.bloor.is.net .cable.rogers.com...
Thanks alot guys for the help.That really clarified alot of thing.
But one more that I am not sure I really got.
In the example I mentiond(b = c).
do b and c each has spearated " identical" objects no matter how many
attributes are there?
ok,what if the object Test has a pointer d that refers to another different object.
will b and c have two different pointers refering to the same object
referred by d previously or two pointers refereeing to two different object? a bit confusing,eh!!


If b and c are objects of your classes, then it's up to you to define how
"=" works. If you don't define the "=" function yourself, then the compiler
gives you one, but it might not do what you want. If you want it to work so
that if the 2 pointers point to the same object then the original objects
are equal, then you should go ahead and write your "=" function to work that
way.
Jul 22 '05 #7
Snake,

Just to make sure you are getting the pointer thing; in C++ when you use
the new() operator, it is exactly the same thing as in JAVA. That is to
say, you have created a reference to an object. Remember when in your
JAVA training they said that JAVA is a language which referes to
everything by reference? well that's what is going on here in C++ as
well. The difference in C++ is that you have even more options.

Evan

Jul 22 '05 #8

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

Similar topics

29
by: RAY | last post by:
Hi , my boss has asked I sit in on an interview this afternoon and that I create some interview questions on the person's experience. What is C++ used for and why would a company benefit from...
13
by: Joe Black | last post by:
Just to inform you guys that i have only like 2 weeks that i took my first classes in c++, and my proffesor now is asking me to solve this problem: /// Using a function create a Win32 Console...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
5
by: Richard | last post by:
Hi, I'm writing an MS Outlook 2000 Addin in C#. I have created C# classes that monitor folder events and do other business logic. Things work fine until I want to exit Outlook. When I exit...
6
by: billr | last post by:
I have developed a small API for taking care of a lot of boiler plate stuff in a multi formed windows application, for example setting up a messaging thread framework. New Forms, in the...
0
by: Gareth Stretch | last post by:
Hi Guys. i am using C#.net connecting to an Access database using OleDbConnection i am using the following select Statement to join 3 tables string strdvds = "SELECT dvd.name,...
92
by: Ray | last post by:
I just moved to another company that's mainly a Java/.NET shop. I was happy to find out that there's a movement from the grassroot to try to convince the boss to use a dynamic language for our...
6
by: bjwillykajilly | last post by:
ok, so we're to right this program(in bluej) that consists of a few classes that will ultimately simulate a simple billfold(consisting of credit cards) a main card class, a drivers license, an id...
1
by: skygenn | last post by:
hi guys im new at c++ program and im 1st year taking up I.T our prof in c++ gave us an assignments but she didnt even explain us how to begin doing it? honestly i have no idea how to begin it...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.