Default konstruktor struct C{ int i; }; struct D{ D( ):i(1){ }

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Advertisements

Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Operator overloading redefine the operations of operators
Template Implicit function overload. Function overload Function overload double ssqq(double & a, double & b) { return(a*b);} float ssqq(float & a, float.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Shape.h Shape Point Circle Cylinder // SHAPE.H
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.
CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.
 Review structures  Program to demonstrate a structure containing a pointer.
C Tokens Identifiers Keywords Constants Operators Special symbols.
PiKKS. Ukratko Općenito Osnovni elementi OOP u Javi Overloading, overriding, virtual method invocation, modifikatori... Exceptions Java GUI Threads.
C++ Streams © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
C++ Programming: chapter 4 – operator overloading 2014, Spring Pusan National University Ki-Joune Li 1.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
C++ Lesson 1.
Variables, Identifiers, Assignments, Input/Output
Function Overloading Can enables several function Of same name
Data types Data types Basic types
LESSON 3 IO, Variables and Operators
Compiler Construction
Reserved Words.
RP3/predavanje08 Ugniježdeni tipovi Iznimke 10/11/2018
Klase i objekti u C#.
Java Hello world !.
Programi,Podaci,Varijable,Računanje- Uvod
RP3/predavanje02 Programski jezik C#: Tipovi 12/11/2018
Objektno programiranje (C++)
Programiranje - Blokovi naredbi i logički tipovi –
Programiranje u VB Uvod
Programiranje u VB Osnove
Petlje FOR - NEXT.
Klasa Konceptualna razlika: Uvodimo ključnu riječ class
null, true, and false are also reserved.
C++ Basics.
KREIRANJE OBJEKATA.
Oрганизација програма
Java Klase (Classes).
Uvod u programiranje - matematika – X predavanje
Arrays and strings -2 (nizovi i znakovni nizovi)
14 UNUTRAŠNJE I ANONIMNE KLASE
Изведене класе Вишеструко извођење Полиморфизам
درس برنامه‌سازي کامپيوتر
Arrays and strings -1 (nizovi i znakovni nizovi)
Klasa grafik.
Objektno orijentisano programiranje
Pristup podacima Izvještaji
תכנות מכוון עצמים ו- C++ יחידה 06 העמסת אופרטורים
MessageBox.
predavanja v.as.mr. Samir Lemeš
Ključne reči,identifikatori, konstante i promenljive
PROGRAMSKI JEZIK PASCAL
C++ Programming: chapter 4 – operator overloading
Osnovni simboli jezika Pascal
Govt. Polytechnic,Dhangar
Variables, Identifiers, Assignments, Input/Output
JavaScript Reserved Words
Iznimke C++ nudi mehanizam izbacivanja i hvatanja iznimaka za postupanje s greškama pri izvršavanju programa. Uporaba tog mehanizma nije nužna jer se.
Default Arguments.
Programming Language C Language.
Review of Function Overloading
Ponavljanje Pisana provjera
Overloading the << operator
Programiranje - Naredbe za kontrolu toka programa – 1. dio
Presentation transcript:

Default konstruktor struct C{ int i; }; struct D{ D( ):i(1){ } D(int ii):i(ii){ } void f(){ static C c3; static D d3; cout<<c3.i<<" "<<d3.i<<endl; } C c1; D d1; int main( ){ C c2; D d2; f(); cout<<c1.i<<" "<<c2.i<<endl; cout<<d1.i<<" "<<d2.i<<endl; return 0;

Imenovani konstruktori class Point { friend ostream& operator<<(ostream& out, const Point& p); public: static Point rectangular(float x, float y); static Point polar(float radius, float angle); private: Point(float x, float y); float _x, _y; };

Imenovani konstruktori #include <cmath> Point::Point(float x, float y): _x(x), _y(y) { } Point Point::rectangular(float x, float y) { return Point(x, y); } Point Point::polar(float radius, float angle) { return Point(radius*cos(angle), radius*sin(angle)); ostream& operator<<(ostream& out, const Point& p) out<<"("<<p._x<<“, "<<p._y<<")"; return out;

Imenovani konstruktori int main( ) { Point p1 = Point::rectangular(1.1, 2.3); Point p2 = Point::polar(1.1, 2.3); cout<<p1<<endl; cout<<p2<<endl; return 0; } // (1.1, 2.3) // (-0.732904, 0.820276)

Konstruktori i iznimke class C{ int a; int *p; public: C( ) try : a(0), p(new int(0)){ } catch (const bad_alloc &e){ ….. } }; Napomena: catch hvata iznimke izbačene i iz inicijalizacijske liste i iz tijela konstruktora.

Pokazivači na članove klase class C{ public: int x,y; string s; C(int xx, int yy, const string& ss):x(xx), y(yy), s(ss){} }; void f(const C *objPtr, const C obj, int C::*p){ cout<<objPtr->*p<<" "<<obj.*p; }

Pokazivači na članove klase int main( ){ C o(1,2,"string"); C *ptr=&o; int C::*memberPtr1=&C::x; int C::*memberPtr2=&C::y; string C::*memberPtr3=&C::s; f(ptr, o, memberPtr2); return 0; }

Pokazivači na članove klase int C::*memberPtr1=&C::x; int C::* - tip pokazivača na član klase memberPtr1 - ime varijable &C::x - inicijalizator

Pokazivači na članove klase int *p=&(ptr->x); // Definicija pokazivača na varijablu tipa int. Sadrži apsolutnu lokaciju varijable u memoriji. int C::*memberPtr1=&C::x; // Definicija pokazivača na član x klase C. Predstavlja relativnu lokaciju, odnosno pomak u odnosu na adresu zadanog objekta. Ne radi se o memorijskoj lokaciji nego o iznosu koji je potrebno dodati na memorijsku lokaciju. To dodavanje se vrši pomoću operatora ->* ili .* . cout<<ptr->*memberPtr1; // Pristupamo lokaciji s adresom ptr i pomičemo se za memberPtr1. cout<<*p<<endl; // Ispisuje isto.

Unutarnje klase Klasa može biti definirana unutar neke druge klase i tada govorimo o unutarnjoj ili ugniježđenoj klasi. Definiranjem klase unutar neke druge klase definiramo tip koji je ugniježđen u dosegu okružujuće klase. Objekti unutarnje klase postoje posve neovisno o objektima vanjske klase

Unutarnje klase class Outer { public: double x; static int level; {      public:          double x;          static int level;          enum Boje { plava, crvena, crna};           typedef double Real;          class Inner {              public:                  Real y;                  double f() { return y*y;}          };

Unutarnje klase int Outer::level = 3; int main( ) { Outer::Inner a; // Objekt unutarnje klase      a.y = 3.0;      std::cout << a.f() << std::endl;      return 0; }

class Out{ int a; class In{ int b; }; int main( ){ Out::In a; } Unutarnje klase class Out{ int a; class In{ int b; }; int main( ){ Out::In a; }

Unutarnje klase class Out{ int a; class In{ int b; }; int main( ){ Out::In a; // greška, tip In je private u klasi Out }

Unutarnje klase class Out{ int a; class In{ int b; }; int main( ){ Out::In a; // greška, tip In je private u klasi Out }

Unutarnje klase Unutarnja klase je gotovo posve neovisna o vanjskoj klasi, jedino je njeno ime skriveno iza imena vanjske klase. Ipak, vrijedi pravilo da unutarnja klasa može dohvatiti statičke varijable, enumeratore i typedef-ove iz vanjske klase (razina pristupa članovima vanjske klase može ovisiti i o kompilatoru).

Unutarnje klase class Outer { public: double x; static int level; enum Boje { plava, crvena, crna };          typedef double Real;          class Inner {              public:                  Real y;                  Boje g() { return crna; }                  Real f() { return y*level;}              private:                  static Real z;          }; int Outer::level = 3; Outer::Real Outer::Inner::z = 3.14; .

inline prvi.cpp inline void f( ){ } void g( ){ } drugi.cpp void g( ){ } // greška

static, extern prvi.cpp int a=1; //globalna varijabla drugi.cpp void print() { cout<<a; } //greška, a nije deklarirana

static, extern prvi.cpp int a=1; //globalna varijabla drugi.cpp extern int a; void print() { cout<<a; }

static, extern prvi.cpp static int a=1; //globalna varijabla drugi.cpp extern int a; //greška, imamo unutarnje povezivanje za varijablu a void print() { cout<<a; }