C/C++ hell(p)

Krakerjak

Senior member
Jul 23, 2001
767
0
0
Ok, i really need to do some studying here.

I'm really confused right now with structures and classes.
And to ice the cake our teacher likes to really confuse the topic by using several unfamiliar coding styles.

If anyone know of any good online resources that really helps for a person relatively new to structures and classes please post them here//
This is just an example of what we are doing, not really meaningful code


typedef struct student
{
char* psurname;
char* pgiven1;
char* pstudnum;
double gpa;
} STUD;

STUD* make_student (void)
{
STUD* pstud = NULL;
char dummy[80];

clrscr ();
printf ("Surname: ");
gets (dummy);

if (*dummy != '\0'); // if null string not entered
{
pstud = (STUD*) malloc (sizeof (STUD));

if (pstud == NULL)
printf ("\n*** Cannot create new student ***");

else
{
psurname = (char*) malloc (strlen (dummy) + 1);
strcpy (pstud->psurname, dummy);

printf ("\nGiven name: ");
gets (dummy);
pgiven1 = (char*) malloc (strlen (dummy) + 1);
strcpy (pstud->pgiven1, dummy);

printf ("\nStudent number: ");
gets (dummy);
pstudnum = (char*) malloc (strlen (dummy) + 1);
strcpy (pstud->pstudnum, dummy);

printf ("\nGPA:");
gets (dummy);
if (strlen (dummy) == 0) // another test for null string
pstud->gpa = 0.0;
else
pstud->gpa = atof (dummy); // string to double
}
}
return (pstud);
}
 

Krakerjak

Senior member
Jul 23, 2001
767
0
0
Originally posted by: singh
This must be C.

It is, i spent all last term doing C and this course starts off with C...all procedural programming , then goes head on into C++ all OOP.
It seems making this switch is kinda making my head spin a bit.

Originally posted by: wizardLRU
I can't really think of anything specific, but I'm sure programmer's heaven will have what you're looking for.

Thanks, ill look into it.
Never been there b4
 

TheDiggler

Senior member
Dec 23, 2002
695
0
0
Just a quick tidbit for you since you're going from C to C++:

Recap from your C class last year: In C, a STRUCTURE is for all intents and purposes a LAYOUT OF A COLLECTION of VARIABLES. When you define a VARIABLE of a particular STRUCTURE NAME, that variable takes up the memory of the ENTIRE LAYOUT of the structure.

In C++, STRUCTURES happen to EXACTLY THE SAME THING as CLASSES, w/ one notable difference: everything declared in a STRUCTURE is PUBLIC by default, everything declared in a CLASS is PRIVATE by default. Of course to understand what that means, you need to be familiar w/ the PUBLIC/PROTECTED/PRIVATE sections of classes.

With that said, in C++, the C capabilities of STRUCTURES do not disappear. If you want to create a STRUCTURE that is a LAYOUT OF A COLLECTION OF VARIABLES, you're free to do so. However, in C++, STRUCTURES are enhanced by the capabilities provided by C++ CLASSES.

In C++, a CLASS is a LAYOUT OF A COLLECTION OF VARIABLES (a.k.a. ATTRIBUTES) as well as a LAYOUT OF FUNCTIONS (a.k.a. METHODS). When you declare an INSTANCE of a class (i.e. when you create a variable of a given CLASS NAME), that variable becomes an OBJECT. What makes it an OBJECT versus some dumb C-Style STRUCTURE? The fact that you can invoke METHODS (i.e. FUNCTIONS) on that OBJECT, methods which are defined in the OBJECT's CLASS LAYOUT. In C, that capability doesn't exist.

Perhaps the above went over your head, perhaps it didn't. Once you understand it though, you'll be 1% closer towards mastering C++! :D
 

Krakerjak

Senior member
Jul 23, 2001
767
0
0
Originally posted by: TheDiggler

Perhaps the above went over your head, perhaps it didn't. Once you understand it though, you'll be 1% closer towards mastering C++! :D

Ain't that the truth...

I think that sunk in, thanks.
I have already gone over the different aspects of classes so i can relate with what u said. Thz :cool:

Any more good tips anyone???
Keep em coming