20-CS-122-001 Computer Science II Spring 2012
Simple Class - A Method Added

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, looking like a struct, with a constructor for initialization
// and a simple method.
class Simple {  
public:
   char one, two, three, four;

   // Constructor
   Simple (char one, char two, char three, char four) {
      this->one = one;
      this->two = two;
      this->three = three;
      this->four = four;
   }

   // A method
   int sum () {  return one + two + three + four;  }
};
   
int main () {
   Simple *a = new Simple(2,4,64,0);
   cout << "\nSum = " << a->sum() << "\n";
}