lec07-class

lec07-class - CSC 1111 Introduction to Computing using C...

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

View Full Document Right Arrow Icon
1 CSC 1111 Introduction to Computing using C++ Classes and Objects
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Questions to be answered Why classes? How to properly define class?
Background image of page 2
3 Classes and Objects Classes A class contains the definition or design for a type of objects. Objects Variables created according to the "design" specified in the class class Date { public : int dd, mm, yy; }; int main() { Date d1, d2; int day, month, year; }
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Use of classes as data containers What are some of the reasons behind using classes? e.g.: Representing a date using a class vs. using three values of type int . What else do we need before we can use the Date objects conveniently in the source program? class Date { public : int dd, mm, yy; };
Background image of page 4
5 Designing Class Using class is all about design Good design More convenient to use later Reusable Improve productivity Easier to maintain Good design Safer to use later Less likely to produce errors Times invested in designing a "good" class can pay off later
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Methods (or Member Functions) Functions defined in a class Belong to objects of the class Invoked through an object of the class class Date { public : int dd, mm, yy; void setDate( int _dd, int _mm, int _yy) { dd = _dd; mm = _mm; yy = _yy; } }; // In some function Date d; d.setDate(1, 1, 2000);
Background image of page 6
7 Methods (or Member Functions) Methods can access/modify data members directly. class Date { public : int dd, mm, yy; void setDate( int _dd, int _mm, int _yy) { dd = _dd; mm = _mm; yy = _yy; } };
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Methods vs. Functions class Date { public : int dd, mm, yy; void setDate( int _dd, int _mm, int _yy) { dd = _dd; mm = _mm; yy = _yy; } }; void setDate(Date &d, int _dd, int _mm, int _yy) { d.dd = _dd; d.mm = _mm; d.yy = _yy; } Methods Functions Insiders Outsiders Attached to objects Not attached to objects Have all access rights Can only access "public" members
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/28/2010 for the course CSC CSC1110 taught by Professor Cjyuan during the Fall '06 term at CUHK.

Page1 / 27

lec07-class - CSC 1111 Introduction to Computing using C...

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

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