20-CS-122-001 Computer Science II Spring 2012
Simple Class - Data Only

Virtual functions, classes, inheritance, lists, queues, stacks, applications

Prev      Next      Lectures      Source

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;

// A simple class that looks and behaves like a struct
class Simple {  
public:
   char one, two, three, four;  
};
   
int main () {
   Simple *a = new Simple();
   a->one = 2;
   a->two = 4;
   a->three = 64;
   a->four = 0;
   
   cout << "\nValue of a.numbers: "
        << (int)a->one << " " 
        << (int)a->two << " "
        << a->three << " "
        << (int)a->four << "\n";
   cout << "Addresses:\n"
	<< " a->one=" << (unsigned long)&a->one
	<< " a->two=" << (unsigned long)&a->two
	<< " a->three=" << (unsigned long)&a->three
	<< " a->four=" << (unsigned long)&a->four
	<< "\n";
   // 00000010 00000100 01000000 00000000
   //     2   +  1024  + 4194304 +  0  = 4195330
   printf("Value of a: %u\n", *(unsigned long*)a);
}