Classes and Objects in C++

Read this text on the implementation of classes, methods, attributes, and driver programs.

6.088 Intro to C/C++

Why objects?

Object-oriented programming (OOP) in C++

  • classes
  • fields & methods
  • objects
  • representation invariant 

Why objects?

At the end of the day... computers just manipulate 0's and 1's


But binary is hard (for humans) to work with.


Towards a higher level of abstraction



There are always trade-offs

High-level languages

  • simpler to write & understand 
  • more library support, data structures

Low-level languages 

  • closer to hardware 
  • more efficient (but also dangerous!)

What about C++?


What are objects? 

Objects model elements of the problem context

Each object has:
  • characteristics 
  • responsibilities (or required behaviors)

Example
Problem Design and build a computer hockey game

Object Hockey player

Characteristics Position, height, weight, salary, number of goals

Responsibilities Pass the puck, shoot, skate forward, skate backward, punch another player, etc.

Another example

Problem Computation modeling in biology

Write a program that simulates the growth of virus population in humans over time. Each virus cell reproduces itself at some time interval. Patients may undergo drug treatment to inhibit the reproduction process, and clear the virus cells from their body. However, some of the cells are resistant to drugs and may survive.

What are objects?

Characteristics?

Responsibilities?


Write a program that simulates the growth of virus population in humans over time. Each virus cell reproduces itself at some time interval. Patients may undergo drug treatment to inhibit the reproduction process, and clear the virus cells from their body. However, some of the cells are resistant to drugs and may survive.

What are objects?

Characteristics?

Responsibilities?


Questions

Why didn't we model an object named Doctor?

Surely, most hospitals have doctors, right?

Why didn't we model an object named Doctor?

Surely, most hospitals have doctors, right?

Doesn't every patient have an age? Gender? Illness?

Symptoms? Why didn't we model them as characteristics? 


Basic OOP in C++

Classes

A class is like a cookie cutter; it defines the shape of objects

Objects are like cookies; they are instances of the class



Classes: Declaration vs. definition


Declaration (.h files)

  • list of functions & fields
  • including functions that the class promises to its client
  • it's like a "contract" 

Definition (.cc files)

  • implementation of functions

Class declaration



Fields (characteristics) 


Methods (responsibilities)


Constructors


Note the special syntax for constructor (no return type!)


Access control: public vs. private 


private: can only be accessed inside the class

public: accessible by anyone


How do we decide private vs. public?



interface: parts of class that change infrequently

(e.g. virus must be able to reproduce)

implementation: parts that may change frequently

(e.g. representation of resistance inside virus)


Protect your private parts!



Why is this bad? 


Access control: public vs. private 



In general,

  • keep member fields as private
  • minimize the amount of public par 28 ts


Access control: constant fields 


Why make it a constant? Why not just declare it as a normal field?


Class definition 


Header inclusion 


Constructor definition 





Remember to initialize all fields inside constructors!


Can also do


Method definition 



Working with objects

Patient class declaration 



Static object allocation 



Calling the constructor 



Deleting statically allocated objects


Automatically destroyed at the end of scope

 

Objects on heap

To allocate an object on heap:

  • use "new" keyword (analogous to "malloc")

To deallocate:

  • use "delete" keyword (analogous to "free")



Dynamic object creation: Example


Using dynamically allocated objects



What happens during destruction?

The destructor is automatically called 


But why didn't we have a destructor forVirus?


Representation invariant

Statements about characteristics of objects

  • defines what it means for an object to be valid
  • e.g. "Every IKEA coffee table must have four legs"


Example


What are the representation invariants for Patient?


Rep. invariant violation


What's wrong with this method?


Preserving rep. invariant


checkRep

  • returns true if and only if the rep. invariants hold true
  • call checkRep at the beginning and end of every public method
  • call checkRep at the end of constructor



Will calling checkRep() slow down my program?

Yes, but you can take them out once you are confident about your code. 



Source: MITOpenCourseware, https://ocw.mit.edu/courses/6-088-introduction-to-c-memory-management-and-c-object-oriented-programming-january-iap-2010/resources/mit6_088iap10_lec04/
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 License.

Last modified: Thursday, April 25, 2024, 2:57 PM