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

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

Prev      Next      Lectures      Source

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

// A class that we may wish to put into a List
class A {
   long long ss_number;
   char *name;

public:
   A (char *name, long long ss_number) {
      this->name = new char[strlen(name)+1];
      strcpy(this->name, name);
      this->ss_number = ss_number;
   }

   long long get_ss_number() { 
      return ss_number;
   }

   char *get_name () { return name; }

   int get_length () { return strlen(name); }

   void set_name (char *name) {
      delete (this->name);
      this->name = new char[strlen(name)+1];
      strcpy(this->name, name);
   }
};

int main () {
   A *a = new A("John Franco", 776655554);
   cout << "Name: " << a->get_name() << " (" << a->get_length() 
	<< ") SS#: " << a->get_ss_number() << "\n";
   a->set_name("This is a really long name");
   cout << "Name: " << a->get_name() << " (" << a->get_length()
	<< ") SS#: " << a->get_ss_number() << "\n";
   //cout << a->ss_number << "\n";
}