Class (1A) Young Won Lim 9/8/14

Similar documents
Accessibility (1A) Young Won Lim 8/22/13

Class (1A) Young Won Lim 11/20/14

Methods (2A) Young Won Lim 10/14/14

Methods (2A) Young Won Lim 12/10/14

Pointers (1A) Young Won Lim 1/9/18

Pointers (1A) Young Won Lim 1/5/18

Pointers (1A) Young Won Lim 11/1/17

Applications of Pointers (1A) Young Won Lim 12/26/17

Pointers (1A) Young Won Lim 1/14/18

Algorithms Bubble Sort (1B) Young Won Lim 4/5/18

Pointers (1A) Young Won Lim 3/5/18

Pointers (1A) Young Won Lim 1/22/18

Functions (4A) Young Won Lim 5/8/17

Day08 A. Young W. Lim Mon. Young W. Lim Day08 A Mon 1 / 27

Function Overview (1A) Young Won Lim 10/23/17

Applications of Structures (1A) Young Won Lim 12/8/17

Functions (4A) Young Won Lim 3/16/18

Overview (1A) Young Won Lim 9/14/17

Overview (1A) Young Won Lim 9/9/17

Pointers (1A) Young Won Lim 2/6/18

Applications of Structures (1A) Young Won Lim 12/4/17

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

Overview (1A) Young Won Lim 9/25/17

Tiny CPU Data Path (2B) Young Won Lim 5/2/16

Pointers (1A) Young Won Lim 2/10/18

Applications of Pointers (1A) Young Won Lim 2/27/18

HW / SW Implementation Overview (0A) Young Won Lim 7/16/16

Monad Background (3A) Young Won Lim 11/20/17

Structure (1A) Component

Applications of Pointers (1A) Young Won Lim 3/14/18

Applications of Pointers (1A) Young Won Lim 3/31/18

Applications of Pointers (1A) Young Won Lim 3/21/18

Variables (2D) Young Won Lim 3/28/18

Applications of Pointers (1A) Young Won Lim 1/5/18

Pointers (1A) Young Won Lim 12/4/17

Applications of Pointers (1A) Young Won Lim 4/24/18

RAM (1A) Young Won Lim 11/12/13

Applications of Pointers (1A) Young Won Lim 4/11/18

Applications of Arrays (1A) Young Won Lim 2/11/17

Memory (1A) Young Won Lim 9/7/17

Pointers (1A) Young Won Lim 10/18/17

Memory Arrays (4H) Gate Level Design. Young Won Lim 3/15/16

Binary Search Tree (3A) Young Won Lim 6/2/18

Type (1A) Young Won Lim 2/17/18

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Pointers (1A) Young Won Lim 10/23/17

Example 3 : using a structure array. Young Won Lim 11/25/17

Applications of Arrays (1A) Young Won Lim 3/15/17

Binary Search Tree (2A) Young Won Lim 5/17/18

Monad (1A) Young Won Lim 6/9/17

Algorithms (7A) Young Won Lim 4/10/17

Example 1. Young Won Lim 11/17/17

Algorithms (7A) Young Won Lim 4/18/17

Arrays (1A) Young Won Lim 12/4/17

Haskell Overview IV (4A) Young Won Lim 10/13/16

Example 1 : using 1-d arrays. Young Won Lim 12/13/17

ELF (1A) Young Won Lim 10/22/14

Polymorphism Overview (1A) Young Won Lim 2/20/18

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Eulerian Cycle (2A) Young Won Lim 4/26/18

Polymorphism (1A) Young Won Lim 8/22/13

Example 3. Young Won Lim 11/22/17

Structure (1A) Young Won Lim 7/30/13

Example 2. Young Won Lim 11/24/17

Side Effects (3A) Young Won Lim 1/13/18

FPGA Configuration (1C) Young Won Lim 12/1/13

Pthread (9A) Pthread

A Sudoku Solver (1A) Richard Bird Implementation. Young Won Lim 11/15/16

Haskell Overview IV (4A) Young Won Lim 10/24/16

Polymorphism (1A) Young Won Lim 8/15/13

Binary Search Tree (3A) Young Won Lim 6/4/18

Binary Search Tree (3A) Young Won Lim 6/6/18

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

Structures (1A) Young Won Lim 12/4/17

File (1A) Young Won Lim 11/25/16

Expressions (2E) Young Won Lim 3/10/18

Operators (2A) Young Won Lim 10/2/13

Side Effects (3B) Young Won Lim 11/27/17

Haskell Overview III (3A) Young Won Lim 10/4/16

Haskell Overview II (2A) Young Won Lim 8/9/16

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

Arrays (1A) Young Won Lim 1/27/17

Structures (1A) Young Won Lim 11/8/16

Monoid (4A) Young Won Lim 8/12/17

Algorithms Overview (1A) Young Won Lim 3/29/18

A Sudoku Solver Pruning (3A)

Monad Background (3A) Young Won Lim 11/8/17

CORDIC Fixed Point Simulation. Young Won Lim 2/28/12

Background Constructors (1A) Young Won Lim 6/30/18

IO Monad (3C) Young Won Lim 8/23/17

Monad (1A) Young Won Lim 6/21/17

Common Gateway Interface CGI

Libraries (1A) Young Won Lim 6/5/17

Monad Background (3A) Young Won Lim 11/18/17

CUDA Parallel Prefix Sum (1A) Young W. Lim 4/5/14

State Monad (3D) Young Won Lim 8/31/17

State Monad Example (3H) Young Won Lim 2/16/18

ARM Assembly Exercise (1B) Young Won Lim 7/16/16

Background Constructors (1A) Young Won Lim 1/22/18

Transcription:

Class (1A)

Copyright (c) 2011-2013 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". Please send corrections (or suggestions) to youngwlim@hotmail.com. This document was produced by using OpenOffice.

Class Definition class Ccircle { int r; Ccircle () { r = 1; Ccircle (int x) { r = x; void setr (int x) { r = x; int getr () { return r; double area () ; ; member functions defined outside the class definition double Ccircle::area () { return 3.14*r*r; A Member Constructor functions Member functions Scope Operator :: Ccircle:: Class 3

Creating Objects class Ccircle { int r; Ccircle () { r = 1; Ccircle (int x) { r = x; void main(void) { Ccircle C1; Ccircle C2(10); default constructor is called Ccircle C1(); void setr (int x) { r = x; int getr () { return r; double area () ; ; object C1 r = 1 object C2 r = 10 double Ccircle::area () { return 3.14*r*r; setr () getr () area () setr () getr () area () Member functions are called from objects Objects have their own member data So member functions access these distinct member data Class 4

Calling Member Functions class Ccircle { int r; Ccircle () { r = 1; Ccircle (int x) { r = x; void setr (int x) { r = x; int getr () { return r; double area () ; ; void main(void) { Ccircle C1; Ccircle C2(10); C1.setR (10); C2.setR (20); C1.setR (10); default constructor is called C2.setR (20); double Ccircle::area () { return 3.14*r*r; object C1 r = 1 10 void setr (int x) { r = x; object C2 r = 10 20 void setr (int x) { r = x; Class 5

Member Functions : called from objects class Ccircle { int r; Ccircle () { r = 1; Ccircle (int x) { r = x; void setr (int x) { r = x; int getr () { return r; double area () ; ; void main(void) { Ccircle C1; Ccircle C2(10); C1.setR (10); C2.setR (20); C1.area () ; C2.area () ; default constructor is called double Ccircle::area () { return 3.14*r*r; Member functions are called from objects Objects have their own member data So member functions access these distinct member data Class 6

Objects and Member Function Calls C1.area (); 3.14*10^2 Ccircle C1; C1.setR (10); object C1 r = 10 setr () getr () area () object C1 r = 10 double area () { return 3.14*r*r; C2.area (); 3.14*20^2 Ccircle C2(10); C2.setR (20); object C2 r = 20 setr () getr () area () object C2 r = 20 double area () { return 3.14*r*r; Class 7

Member Function Call class Ccircle { int r; Ccircle () { r = 1; Ccircle (int x) { r = x; void setr (int x) { r = x; int getr () { return r; double area () ; ; possible implementation : void Ccircle::setR (Ccircle *this, int x) { this->r = x; r = x; void main(void) { Ccircle C1; Ccircle C2(10); C1.setR (10); C2.setR (20); C1.area () ; C2.area () ; Ccircle::setR (&C1, 10); passing a pointer hidden to a programmer Class 8

Access Specifier class Ccircle { int r; Ccircle () { r = 1; Ccircle (int x) { r = x; void setr (int x) { r = x; int getr () { return r; double area () ; ; class Ccircle { int r; default private: Ccircle () { r = 1; Ccircle (int x) { r = x; void setr (int x) { r = x; int getr () { return r; double area () ; ; void main(void) { Ccircle C1; Ccircle C2(10); C1.setR (10); C2.setR (20); C1.r = 13; C2.r = 24; Class 9

Member Function Definition within a class int func1( ) { mem2 = 10; func2 (); mem3 = 10; func3 (); int func2( ) { mem1 = 10; func1 (); mem3 = 10; func3 (); int func3( ) { mem1 = 10; func1 (); mem2 = 10; func2 (); member functions of the same class member functions of the same class member functions of the same class class CC { private: int mem1; int func1(); protected: int mem2; int func2(); int mem3; int func3(); ; Each members can be accessed by other members of the same class Class 10

Member Function Definition within a derived class The members of a derived class can access public and protected members of the base class class CC { private: int mem1; int func1(); protected: int mem2; int func2(); int mem3; int func3(); ; class EE : public CC { int func4( ) { mem2; func2 (); mem3; func3 (); ; Class 11

Member Function Call from objects void main(void) { CC C1; C1.mem3; C1.func3 (); int foo(cc *X) { X->mem3; X->func3 (); class DD { int faa(cc *Y) { Y->mem3; Y->func3 (); ; the main function C-style functions member functions of other classes class CC { private: int mem1; int func1(); protected: int mem2; int func2(); int mem3; int func3(); ; Only public members can be accessed Class 12

Class Structure Class 13

Class Structure Class 14

References [1] W Savitch, Absolute C++ [2] P.S. Wang, Standard C++ with objected-oriented programming [3] http://www.cplusplus.com