Jzyk C++ cz 4. Jarosław Gramacki Instytut Informatyki i Elektroniki ' (%)* ) ' &+ (,% &*-

Similar documents
Visual C Tutorial

Jzyk C++ cz 7. Jarosław Gramacki Instytut Informatyki i Elektroniki

5 CLASSES CHAPTER. 5.1 Object-Oriented and Procedural Programming. 5.2 Classes and Objects 5.3 Sample Application: A Clock Class

Deitel Dive-Into Series: Dive Into Microsoft Visual C++ 6

C++ Support for Abstract Data Types

Comp151. Definitions & Declarations

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

Object Oriented Software Design II

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

William Paterson University Department of Computer Science. Microsoft Visual C++.NET Tutorial Spring 2006 Release 1.0

CS107L Handout 02 Autumn 2007 October 5, 2007 Copy Constructor and operator=

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

An Incomplete C++ Primer. University of Wyoming MA 5310

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

Appendix M: Introduction to Microsoft Visual C Express Edition

Development of a Campus Routing System Praxis der Software-Entwicklung

Object Oriented Software Design II

Using Classes in C++

C++FA 3.1 OPTIMIZING C++

Chapter 13 Storage classes

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

7.7 Case Study: Calculating Depreciation

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

C++ Crash Kurs. C++ Object-Oriented Programming

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!


Propedéutico de Programación

Software Engineering Techniques

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Cpt S 223. School of EECS, WSU

An API for Reading the MySQL Binary Log

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

C++FA 5.1 PRACTICE MID-TERM EXAM

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Cours de C++ Utilisations des conteneurs

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Syllabus OBJECT ORIENTED PROGRAMMING C++

Basics of I/O Streams and File I/O

CSE 221: Software Development Using Components. Getting Started

Appendix K Introduction to Microsoft Visual C++ 6.0

C++ INTERVIEW QUESTIONS

! " # $ %& %' ( ) ) *%%+, -..*/ *%%+ - 0 ) 1 2 1

Goals for This Lecture:

Pointers and Linked Lists

Data Structures and Algorithms V Otávio Braga

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

13 Classes & Objects with Constructors/Destructors

Ch 7-1. Object-Oriented Programming and Classes

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

Object Oriented Software Design II

C++ Language Tutorial

C++ Overloading, Constructors, Assignment operator

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

C++ Input/Output: Streams

Stacks. Linear data structures

Seaport A Portable and Efficient Process-oriented Simulator

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

EXEC SQL CONNECT :userid IDENTIFIED BY :passwd;

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Binary storage of graphs and related data

CISC 181 Project 3 Designing Classes for Bank Accounts

Data Structures, Practice Homework 2, with Solutions (not to be handed in)

Bevezetés a C++ Standard Template Library-be

Note: Syntactically, a ; is needed at the end of a struct definition.

Chapter 5 Functions. Introducing Functions

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

C Strings and Pointers

Sequential Program Execution

Compiler Construction

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

1. The First Visual C++ Program

C++ Programming Language

The Web Server. 2 Implementation. 2.1 The Process Behind a Web Page Request. Shana Blair and Mark Kimmet

St S a t ck a ck nd Qu Q eue 1

Lab 2 - CMPS 1043, Computer Science I Introduction to File Input/Output (I/O) Projects and Solutions (C++)

While Loop. 6. Iteration

Answers to Review Questions Chapter 7

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

Basics of C++ and object orientation in OpenFOAM

J a v a Quiz (Unit 3, Test 0 Practice)

Lecture Data Types and Types of a Language

CmpSci 187: Programming with Data Structures Spring 2015

El Dorado Union High School District Educational Services

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

Functions and Parameter Passing

1. Polymorphism in C++...2

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Advanced Data Structures

DATA STRUCTURE - STACK

Ada. Randki (spotkania) w Adzie. Model. U Ady. Zasady. task Buffer is entry Append (I: in integer); entry Take (I: out integer); end Buffer;

# Demo: jak to działa?

CS107L Handout 04 Autumn 2007 October 19, 2007 Custom STL-Like Containers and Iterators

Class 16: Function Parameters and Polymorphism

4D Plugin SDK v11. Another minor change, real values on 10 bytes is no longer supported.

Transcription:

Jzyk C++ cz 4 Jarosław Gramacki Instytut Informatyki i Elektroniki # $ %& % ' (%)* ) ' &+ (,% &*-.,& & * * *void main() Stack s1; s1.init(); s1.push( 5 ); s1.print_stack(); Stack s1;??? s1.push( 5 ); s1.print_stack(); ',%& & /*. s1 &% & ) & '.&00 &+ & %*. (,1init()" ' & (& %.(+*)(&( ) &), & 0. +" ', 0 &. ) ', 0 *. %&)./,% ' *23. (& ),&4 %&%+&*"2!" 2

# ' % &&((&,+)& ' &)4 void"&,)inline ' %.&*%*&%%& )&))56 #include <iostream.h> const int MAX_SIZE = 5; enum stack_state OK, FULL, EMPTY; class Stack Stack(); // Konstruktor klasy Stack. void push( int ); int pop(); void print_stack(); int ile_elementow(); int table[ MAX_SIZE ]; int top; stack_state state; ; Stack s1;??? s1.push( 5 ); s1.print_stack(); ) %%& // Konstruktor: inicjuje strukture Stack. inline Stack::Stack() top = 0; state = EMPTY;!" 3 # ' &)4 ' ))"%. /*" class Stack Stack(); Stack( int ); Stack( int, int ); void push( int ); int pop(); void print_stack(); int ile_elementow(); int table[ MAX_SIZE ]; int top; stack_state state; ; // Konstruktor bez argumentu. inline Stack::Stack() top = 0; state = EMPTY; // Konstruktor z jednym argumentem. inline Stack::Stack( int _element ) table[ 0 ] = _element; top = 1; state = OK; // Konstruktor z dwoma argumentami. Stack::Stack( int _elem1, int _elem2 ) top = 2; state = OK; push( _elem1 ); push( _elem2 );!" 4

# ' &+& )0 &%& ( ) " &/ Stack s1; // Wywoluje Stack::Stack(). cout << "s1 :" << endl; s1.push( 5 ); s1.print_stack(); Stack s2( 10 ); // Wywoluje Stack::Stack( int ). cout << "s2 :" << endl; s2.print_stack(); Stack s3( 15, 20 ); // Wywoluje Stack::Stack( int, int ). cout << "s3 :" << endl; s3.print_stack(); ' **%&%*+ %& & &. +&%(%new Stack s1 = Stack(); Stack s2 = Stack( 10 ); Stack s3 = Stack( 10, 20 );!" 5 # ' &+.* /+ %4 class Data // Konstruktor trójargumentowy. Data( int _dzien, int _miesiac = 10, int _rok = 2000 ); int dzien, miesiac, rok; ; Data d1(26, 01, 2000); Data d2; // BLAD, nie ma konstruktora bezparametrowego Data d3 (12) // OK, data: 12-10-2000 Data d4 = d1; // OK, inicjalizacja innym obiektem ' )) & )*% )(* &&& ) ") ' /&&* $% )) )0 " ' %% )(.,& % &%, ( %&& ( & %&& )(%%char *str"!" 6

# '.. +. ) &)4.&%. +,/.&*+" '. ) &/ class Data // Konstruktor trójargumentowy. Data( int _dzien, int _miesiac = 10, int _rok = 2000 ); // Konstruktor bezparametrowy Data(); int dzien, miesiac, rok; ; Data dtab1[3]; Data dtab2[3] = // wywołano konstruktor domylny // (zakładamy, e istnieje) Data(12, 08, 2003), Data(15), Data() ;!" 7 # '. & & &&/%% & ) & 7 )(2 ' %&%.. +*(%) /,%% ) Stack *ps1 = new Stack(); Stack *ps2 = new Stack( 10 ); Stack *ps3 = new Stack( 10, 20 ); Data *pd1 = new Data(26); // data: 26-10-2000 Stack *ps5 = new Stack[5]; // pojawi si błd, gdy NIE bdzie istniał // konstruktor domylny!" 8

# *+&" ' *)( & 0 &0&4 %& &)%, "&. %&./,%.,& )& %+82" ' %.,& )*%&. "%&)./,% '. +*.0)(,%&9& ) " & %&&9&,%*" '. +0)(,% ) main() &. &(&) main()"!" 9 # *+& " ' % &&((&,+)&%%&&( & " ' / &) +") ) ' %. &&&" 3./,%& ( ' &*)&&*),&* *),& "%,/..)**$ %% %char*)!" 10

# class String String( const char* _str ); String( int _size ); ~String(); // Destruktor klasy String. void set( const char* _str ); // seter const char* get(); void print(); int size; char* str; ;.. );::) *% & ) "7)&).* % + // geter // Destruktor obiektu String zwalnia // pami przydzielona // za pomoc operatora new nalecego // do konstruktora. inline String::~String() delete [] str; String s( "Obiekt globalny" ); // Konstruktor obiektu w oparciu o łacuch String::String( const char* _str ) size = strlen( _str ); str = new char[ size + 1 ]; strcpy( str, _str ); // Rezerwacja pamici dla obiektu String String::String( int _size ) size = _size; str = new char[ size + 1 ]; *str = '\0'; str[ size ] = '\0'; s.print(); String s1( "Obiekt lokalny" ); s1.print(); cout << s1.get() << endl; String s2( 12 ); s2.set( "Test obcinania" ); s2.print();., : &+!" 11 # $.&* %!" 12

# ) &)%&% % ' ),&;::+ &, ) &) & )&%&% )& ) 4 ),&<3=;3 *4 *&+ " const int i1 = 7; // OK, inicjalizacja i1 = 12; // BLAD, przypisanie wartoci zmiennej const int const int i2 // BLAD, dla const konieczna inicjalizacja '.,% "&9&,,% &*1 class String2 String2( int _size ); ~String2(); void set( const char* _str ); const char* get(); void print(); const int size; char* str; ; &+/*,& & 1 _size size // pole size jest teraz const. // Zarezerwowanie pamici dla String2. String2::String2( int _size ) size = _size; // BLAD: przypisanie obejmuje dan const. str = new char[ size + 1 ]; *str = '\0';!" 13 # ) &)%&% % $ ) &)( ' &(. + / &,1 ) &)%+. )%, %+" %&% %4 )&%,&>?" ' & ) &) &/.%%&&, ) &)( ' &))(,4 &) & ) &%%" 5& )%& '. (& ) &/& 0%+. class String2 String2( int _size ); ~String2(); void set( const char* _str ); const char* get(); void print(); const int size; char* str; ; && )/ % ) // Rezerwuje pamiec dla obiektu String2. String2::String2( int _size ) : size( _size ) // Inicjalizacja pola //Faza przypisania. str = new char[ size + 1 ]; % 4/% *str = '\0';!" 14

#. '. ) & "*(./& const const String s( "Zmienna globalna" ); const Data d1(26, 01, 2000); ' )3.,& /). const@),&..(% const String s1( "Staly obiekt String" ); cout << s1.get() << endl; ' =()1get()"+ )(. ) %)( %. )("A 0/. const A ) & 0 ) %) + /!" 15 # %+)%&%& C *& %) )(, class String String( const char* _str ); String( int _size ); ~String(); void set( const char* _str ); const char* get() const; // Funkcja składowa const B)+3 )(. &&%./)const @& (/.,+ void print() const; // Funkcja składowa const int size; char* str; ; //Zwraca wskanik do łacucha znakowa ASCII zawartego w obiekcie String. inline const char* String::get() const // Nie zapomnij wstawi const. return str; //Wyswietla obiekt String. inline void String::print() const // Nie zapomnij wstawi const. cout << str << endl;.&)%) %).,).( const String s1( "Staly obiekt String" ); cout << s1.get() << endl;!" 16

# ' %&%&&*+,%) + + +&* )(..) 0" '. +const 3$"/+ +)const ',/ 4/& 00. * %+.)& ) &/ ' /%%)9&. &, ' @&1,%)"!" 17 # ' %%&+0 +. +const ) const // time.h class Time Time( int = 0, int = 0, int = 0 ); // default constructor // set functions void settime( int, int, int ); // set time void sethour( int ); // set hour void setminute( int ); // set minute void setsecond( int ); // set second // get functions (normally declared const) int gethour() const; // return hour int getminute() const; // return minute int getsecond() const; // return second // print functions (normally declared const) void printuniversal() const; // print universal time void printstandard(); // print standard time int hour; // 0-23 (24-hour clock format) int minute; // 0-59 int second; // 0 59 ;!" 18

# // time.cpp #include <iostream> #include "time.h" // set hour, minute and second values void Time::setTime( int hour, int minute, int second ) sethour( hour ); setminute( minute ); setsecond( second ); // return hour value int Time::getHour() const return hour; // print Time in standard format void Time::printStandard() cout << ( ( hour == 0 hour == 12 )? 12 : hour % 12 ) << ":" << setfill( '0' ) << setw( 2 ) << minute << ":" << setw( 2 ) << second << ( hour < 12? " AM" : " PM" );!" 19 # // main.cpp // Attempting to access a const object with // non-const member functions. #include <iostream> #include "time.h" int main() Time wakeup( 6, 45, 0 ); // non-constant object. 3$D& &%.. D%+%+ const Time noon( 12, 0, 0 ); // constant object // OBJECT MEMBER FUNCTION wakeup.sethour( 18 ); // non-const non-const E.E+0 %&%+ noon.sethour( 12 ); // const non-const // BŁD, modyfikacja // obiektu const wakeup.gethour(); // non-const const noon.getminute(); // const const noon.printuniversal(); // const const noon.printstandard(); // const non-const // BŁD // pritnstandard NIE const return 0;!" 20

# &% 8 ' String% %8 " char *str ' &%8. )& 4.," )%& /.,+ String s1( "String 1" ); String s2( "String 2" ); s2 = s1; //??? ' (&. s1&)),8.%, & )(*% "String 1" ),.. s2& )(% "String 2" ' % ) s2 = s1;&, str s1 String 1 \0 str s2 String 2 \0 String 1 \0 String 2 \0 po s2 = s1 str s1 str s2!" 21 # &% 8 ' %& ) (,&) )"&%. %,/ &&&EE$ *))) & & E %, &%*E".. & )(8 **% )&. +.,& )%&.)* %,/&(%&&% * *. 2 C%1 ))*. %%%&&String::set()" )*. &*&*., &2" ' F& (& 1 4&0,%%&% =)%&%%% %& /%& (/"& *%.= * C (* )(),operator=() =&&*+& (&&%& ( %+.,( & *!" 22

# %& ( %G ' ),&;::%&&,&0,% 0%+),& &4 %+*+ ',),operator=()+)&.,& % &4 %, &)%&&str& % 8 class String String( const char* _str ); String( int _size ); ~String(); void set( const char* _str ); const char* get() const; void print() const; String& operator=( const String& _string ); // Operator przypisania. int size; char* str; ; String& String::operator=( const String& _string ) if( this == &_string ) // Sprawdza s1 = s1; return *this; delete [] str; // Usuwa stary łacuch. size = _string.size; str = new char[ size + 1 ]; // Przydziela niezbdna pami. strcpy( str, _string.str ); // Kopiuje zawarto łacucha. )../ ) %s1 = s1; return *this; %+)*,))&!" 23 # %& ( %G ' &. )(+ s2 = s1; s2.operator=(s1); // zwykłe wywołanie funkcji // o nazwie operator= %G)%$* ) %& ()+ @ &*. &&+*)),%G"* *))!" 24

# %& ( %G ' (& )90%) )0 ) %&% & & &)4 * &))&*%%&% &*& 0%0%+ String s1( "String 1" ); String s2( "String 2" ); String s3( "String 3" ); s3 = s2 = s1; )1%+) ) %& ' %& & )&& /*) )+)" %G)(&% " s3.operator=(s2.operator=(s1)); ' /&8 *) operator=())4/& &%%& * ) operator=(). 5&&&%&& ),operator=() %./%))4 ))) ' *4)) ) operator=() *(,%)(1 AClass& AClass::operator=(const AClass&);!" 25 # % )( ' %.()(%,%& String s2 = s1; // uywamy domylnego // konstruktora kopiujcego ' %)& & ) &)( 3%&%,) 3%%&% % )( ',%% *)% & )% )( )(& %*. ). (&%)4. ))% )(. E%% %E" ' %% *4**(,%)(1 AClass (const AClass &); ' *)& &% 8 String"(% ( * &%.)% &4 )&%%&% & (&.%& ( %=%%&& )) operator=()" ' & (&.*%..,& % %& % )(*!" 26

# % )( class String String( const char* _str ); String( int _size ); String( const String& _string ); // Konstruktor kopiujcy. ~String(); void set( const char* _str ); const char* get() const; void print() const; String& operator=( const String& _string ); // Operator przypisania. int size; char* str; ; //Konstruktor kopiujacy. String::String( const String& _string ) size = _string.size; str = new char[ size + 1 ]; // Przydziela potrzebna pami. strcpy( str, _string.str ); // Kopiuje zawarto łacucha. 3 * %&&)% )(**%&&4/ H % )(*&9&,)4 9&(%,, I&*-)% *%)%!" 27 # % )( %& ( %G ' %) 0)% )%%+.&. /& ) %+" s2 = s1; // ZABRONIONE String s4 = s3; // ZABRONIONE ' && ) operator=() & % )(*J 0 % "%* I &.&% &.,& && 0) private!" 28

# % )( %& ( %G class String String( const char* _str ); String( int _size ); ~String(); void set( const char* _str ); const char* get() const; void print() const; String& operator=( const String& _string ); // Operator przypisania. String( const String& _string ); // Konstruktor kopiujcy int size; char* str; ; String s1( "Pierwszy lancuch" ); String s2( "Drugi lancuch" ); String s3( "Trzeci lancuch" ); String s4 = s3; // BLAD funkcja String::String(const String&) // jest niedostpna s3 = s2 = s1; // BLAD funkcja String:operator=(const String&) // jest niedostpna!" 29 # % )( %& ( %G (&&)+ & )%,/!" 30