C++ struct vs class

This is an example to show the similarity between a struct and a class

  #include <iostream>using namespace std;

int main()


struct employee

{char *name;

int age;

double salary;


employee e257 = {”raja“,25,23519.78};

cout<< e257.name<<”\t”<<e257.age

<<”\t”<<e257.salary <<endl;

class Employee { public:      char *name;
     int age;double salary; };

Employee E257 = {”raja”,25,23519.78};

cout<< E257.name<<”\t”<<E257.age

<<”\t”<<E257.salary <<endl;

1) All members of a struct are public by default 
2) All members of a class are private by default 
3) For a class member to be public, it has be 
declared public with the keyword "public".  
Advantage of using classes 
1) Member variables are private and protected. 
2) Constructors functions(declared public) can be 
used  to initialize and modify values of that private variables. 
See this page for Constructors:  Constructors & Destructors </pre
 Download here.struct.doc

Pointers in C – Easy way to remember

//          pointer_example.c
#include <stdio.h>
int main(void)
int k;                 // variable type declaration
k = 2;                // assignment statement
//          2 is rvalue (value in the right hand side of "=")
//          k is lvalue (value in the left hand side of "=")
int *ptr;              // pointer to store integer values
ptr = &k;           // address of k
//          *ptr is rvalue
//          ptr is lvalue
printf("value stored in k is %d\n", *ptr);
//          k is name of variable;  k is an unmodifiable lvalue
//          ptr is name of address; ptr is a modifiable lvalue
//          value stored in k is 2

/* The size of memory required to store an address depends on the system. Some computers might require special handling to hold a segment and offset under certain circumstances. The actual size required is not too important so long as we have a way of informing the compiler that what we want to store is an address. Such a variable is called a pointer variable.

1) To store a variable, we need to know how many bits/bytes would be required to hold the value.

2) To store address of a variable, we need not know the number of bits/bytes would be required to hold the address. It is machine dependent.

3) Type declaration for a pointer indicates what type of variable will be stored in that address.


/* Pointers vs Arrays

// In C a string is an array of characters terminated with a binary zero character

(written as slash0.JPG).

char my_string[40];

my_string[0] = ‘T’;

my_string[1] = ‘e’;

my_string[2] = ‘d’:

my_string[3] = ‘slash0.JPG‘;

// C permits

my_string[40] = “Ted”;

/* In the above example, my_string has 37 null characters in it, since the size of the array is 40 which is a constant. Here the lvalue is unmodifiable.

This can be avoided if a pointer is used instead. Here the lvalue is modifiable.


//	Pointers to structures
typedef struct student
 int age;
 int studentNumber;
 float averageGrade;
 char name[32];
int main()
 //a STUDENT variable
 STUDENT mary;
 //a pointer to a STUDENT variable
 STUDENT *pupil;
//assign some values
 mary.age = 14;
 mary.studentNumber = 5678;
 mary.averageGrade = 73;
//tell pupil to point at mary
 pupil = &mary;
//Notice in the examples below the use of -> instead of (*pupil).age
//we can now access members of the mary structure using the pupil pointer
 printf("Mary's student number is %d\n", pupil->studentNumber);
//we can also do assignment and all other normal variable operations
 pupil->averageGrade = 86.2;
printf("Mary's average grade is %f\n", mary.averageGrade);
return 0;

Download here.