010 - 10 C+ 90 Object-Oriented Programming OOP 10.1 10.1.1...

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

View Full Document Right Arrow Icon
10 C++ 9 0 Object-Oriented Programming O O P 10.1 10.1.1 = + 1. C + + 2. 3. 4. 5.
Background image of page 1

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

View Full DocumentRight Arrow Icon
1 0 - 1 10.1.2 C++ C + + C C + + [ 10-1] #include "iostream.h" #define SIZE 100 / / class stack / * * / { int stck[SIZE]; / * * / int top; / * * / p u b l i c : void init(void); / * * / void push(int i); / * * / int pop(void); / * * / } ; / / void stack: i n i t ( v o i d ) / * * / { t o p = 0 ; / * * / } / / void stack push(int i) { if (top= =SIZE) { cout<<"The stack is full!"; / * * / r e t u r n ; 2 0 6 C 10-1
Background image of page 2
} s t c k [ t o p ] = i ; /* * / t o p + + ; / * 1 * / } / / int stack p o p ( v o i d ) { if (top= =0) { cout<<"The stack is underflow!";/ * * / return 0; } top- -; / * 1 * / return stck[top]; / * * / } void main(void) { stack stack1 s t a c k 2 ; / / s t a c k 1 . i n i t ( ) ; / * * / s t a c k 2 . i n i t ( ) ; s t a c k 1 . p u s h ( 1 ) ; / / s t a c k 1 1 stack2.push(2); // s t a c k 2 2 s t a c k 1 . p u s h ( 3 ) ; s t a c k 2 . p u s h ( 4 ) ; cout<<stack1.pop()<<" "; // s t a c k 1 cout<<stack2.pop()<<" "; cout<<stack1.pop()<<" "; cout<<stack2.pop()<<"\n "; } s t a c k s t a c k s t c k t o p i n i t p u s h p o p C C struct c l a s s s t a c k s t a c k C + + s t a c k s t a c k 1 s t a c k 2 s t a c k 1 s t a c k 2 3 4 1 2 10.2 C++ C + + C p r i n t f s c a n f C + + [ 10-2] #include <iostream.h> m a i n ( ) 1 0 C + + 2 0 7
Background image of page 3

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

View Full DocumentRight Arrow Icon
{ c o u t < < " H e l l o World !"; } C + + i o s t r e a m . h c o u t C + + < < c o u t H e l l o Wo r l d ! H e l l o W o r l d ! [ 10-3] #include <iostream.h> #include <math.h> m a i n ( ) { float s s 1 a 1 a 2 a 3 ; // a1 a 2 a 3 s // s1 c o u t < < " a 1 = " ; c i n > > a 1 ; / * * / c o u t < < " a 2 = " ; c i n > > a 2 ; c o u t < < " a 3 = " ; c i n > > a 3 ; / / { s 1 = ( a 1 + a 2 + a 3 ) / 2 ; s = s q r t ( s 1 * ( s 1 - a 1 ) * ( s 1 - a 2 ) * ( s 1 - a 3 ) ) ; / / c o u t < < " a r e a = " < < s ; c o u t < < " \ n " ; } e l s e c o u t < < " e r r o r ! " ; return 0; } c i n C + + > > c i n a 1 = 3 a 2 = 4 a 3 = 5 a r e a = 6 10.3 C++ 2 0 8 C
Background image of page 4
10.3.1 1. class { p r i v a t e : p r o t e c t e d : p u b l i c : } [ 10-4] C++ #include <stdio.h> class exam1 / / { p r i v a t e : / / int x y ; / / p u b l i c : / / void init(); / / float average(); void print(); } ; void exam1::init() / / { x = 3 ; y = 4 ; } float exam1::average() / / {return (x+y)/2.0; } void exam1::print() / / { p r i n t f ( " \ n x = % d y = % d a v e r = % 7 . 2 f \ n " x y a v e r a g e ( ) ) ; } m a i n ( ) / / { exam1 obj; / / o b j . i n i t ( ) ; / / o b j . p r i n t ( ) ; / / return 0; } 1 0 C + + 2 0 9
Background image of page 5

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

View Full DocumentRight Arrow Icon
c l a s s e x a m 1 x y i n i t ( ) a v e r a g e ( ) p r i n t ( ) C + + p r i v a t e C + + protected x = 3 y=4 aver= 3.50 2) class exam1 / / { p u b l i c : float average(); void print(); p r i v a t e : int x y ; p u b l i c : void init(); } ; { } 4) class exam1 / / { p u b l i c : float average() { return (x+y)/2.0; } void print() { p r i n t f ( " \ n x = % d y = % d a
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/05/2010 for the course FINANCE AN FRE6851 taught by Professor Gallagher,evan during the Spring '09 term at NYU Poly.

Page1 / 33

010 - 10 C+ 90 Object-Oriented Programming OOP 10.1 10.1.1...

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

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