lecture14

Lecture14 - Wednesday November 24th Str uctur e Revi ew M or e on Objects and Cl asses i n C A l i ttl e mor e on poi nter s New and Del ete

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Wednesday, November 24 th Structure Review More on Objects and Classes in C++ A little more on pointers: New and Delete
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Structure Review truct PERSON string name; int age; ; truct FAMILY PERSON mother; PERSON father; PERSON child; string last_name; // for the entire family ; nt main(void) FAMILY my_family, *ptr, other; // Q1: Set my_family ’s mother’s name to Linda // Q2: Set ptr to point to my_family // Q3: Set the age of my_family ’s father to 55, using ptr // Q4: Set the other family equal to my_family
Background image of page 2
struct PERSON { string name; int age; }; PERSON get_person(void) { PERSON temp; cin >> temp.name; cin >> temp.age; return(temp); } int main(void) { PERSON me; me = get_person(); cout << me.name; } Structure Review What is printed out if the user types “Voldemort” for the name?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
struct foo { int geek; char nerd; float goober; }; void func(foo achoo) { cout << achoo.geek << endl; achoo.nerd = ‘C’; } void main(void) { foo a = {10,’A’,3.14159}; func(a); cout << a.nerd << endl; } Structure Review What is printed by this program? Let’s work it out on the chalk board.
Background image of page 4
Classes and Member Functions You can define your member functions directly inside your class. class Rect { public: int width; int height; }; private: Or you can define your outside the class too. int GetArea() { int area = width * height; return(area); } void Initialize(int stW, int stH) { width = stW; height = stH; } { } { }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
class Rect { public: int width; int height; }; private: int GetArea() void Initialize(int stW, int stH) { int area = width * height; return(area); } { width = stW; height = stH; } ; ; These are basically prototypes now… ( don’t forget the semicolons ) And we have to make a small change to our actual functions too… GetArea() Initialize(int stW, int stH) We place the class’s name followed by two colons between the return type and the function name. int main(void) { Rect r; r.Initialize(10,20); / / OK cout << r.GetArea (); / / OK } :: :: Classes and Member Functions
Background image of page 6
Both are The Same! class Rect { int width; int height; }; private: int GetArea() { int area = width * height; return(area); } void Initialize(int stW, int stH) { width = stW; height = stH; } public: class { }; int GetArea(); void Initialize(int stW, int stH); int Rect::GetArea() { } void Rect::Initialize(int stW, int stH) { } class declaration class implementation
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Reduce, Reuse, Recycle Once you have designed , programmed and tested a useful class, you can reuse it over and over in all of your programs. When you’re done programming a new class, do the following: 1. Place your class declaration into a .H file. 2. Place your class implementation (methods defined outside the class) into a .CPP file. 3. To re-use your class, add both files (.H and .CPP) to your VC.NET Solution , and remember to include your class’s header file in your other cpp/ h files! This is the idea behind object oriented programming.
Background image of page 8
A New Class: Wallet class Wallet { public : void Init(); void AddBill(int amt); bool RemoveBill(int amt); int GetTotal(void); private : short num1s, num5s; }; void Wallet::Init() { num1s = num5s = 0; } void Wallet::AddBill(int amt) { if (amt == 1) num1s++; else if (amt == 5) num5s++; } bool Wallet::RemoveBill(int amt) { if (amt == 1 && num1s > 0){ num1s--; return(true); } if (amt == 5 && num5s > 0){ num5s--; return(true); } return(false); } int Wallet::GetTotal(void) { Let’s start by reviewing a C++… Class Implementation / / class declaration wallet.h wallet.cpp
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
#include “wallet.h”
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/20/2009 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Page1 / 34

Lecture14 - Wednesday November 24th Str uctur e Revi ew M or e on Objects and Cl asses i n C A l i ttl e mor e on poi nter s New and Del ete

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online