Computer Programming - strona 2

note /search

Defining methods

  • Politechnika Śląska
  • Computer Programming
Pobrań: 0
Wyświetleń: 721

Defining methods  Inside class declaration class person { … void input() { cinageFNameLName; } // semicolon not required … };  a method defined inside a class declaration is inline by default  outside of the class: - use the scope operator - by default, such method is not inline void person :: s...

Derived class

  • Politechnika Śląska
  • Computer Programming
Pobrań: 0
Wyświetleń: 637

Derived class  adult contains fields and methods: char * name, * surname — inherited, inaccessible for the class adult char *nr_ID_card — private in adult print_name(), print_surname () — public, inherited from person person::print_all() — public, inherited, hidden: use scope operator (::) to acce...

Friend

  • Politechnika Śląska
  • Computer Programming
Pobrań: 0
Wyświetleń: 679

Friend  by declaring a ”friendly” code we allow accessing protected and private members of the class class A { friend C::theMethodNoArg(); // specific method (if overloaded, then with specific args) friend class B; // concerns whole declaration of...

Initialization list

  • Politechnika Śląska
  • Computer Programming
Pobrań: 0
Wyświetleń: 644

Initialization list  in a definition of a constructor (and only there) we may use an initialization list T() : member(initializer) [, member(initializer) …] { } class point { double x, y; public: // … point():x(0.0), y(0.0) {}; }; inline point::point(double d) :x(d), y(d) { // all done } point::po...

Matching overloaded functions

  • Politechnika Śląska
  • Computer Programming
Pobrań: 0
Wyświetleń: 679

Matching overloaded functions  no more than one conversion per argument  pick lowest match level, ambiguity is an error 1. exact match (no conversion, but table to pointer, function to pointer...

Memory management

  • Politechnika Śląska
  • Computer Programming
Pobrań: 14
Wyświetleń: 665

Memory management  allocating memory: operator new  syntax: new Type  example: int * pi = new int; // in C: pi = (int*)malloc(sizeof(int))  deallocating: operator delete  syntax: delete pointe...

Operators

  • Politechnika Śląska
  • Computer Programming
Pobrań: 0
Wyświetleń: 721

Operators  What is an operator a = b.val 4 + c * d[ e++ ]  operator defines operations performed on its arguments and the type of the resulting expression - similarly to functions  in C++, we may define operators to be used with objects (at least one argument of the operator should be an object...

Order of calling constructors and Destructors

  • Politechnika Śląska
  • Computer Programming
Pobrań: 0
Wyświetleń: 441

Order of calling constructors and Destructors  Constructors 1. base class (classes in order of declaration) 2. class members in order of declaration 3. constructor's body  Destructors - simply opposite order 1. destructor's body 2. destructors of class members (order opposite to declaration) 3. d...

Overloaded functions

  • Politechnika Śląska
  • Computer Programming
Pobrań: 0
Wyświetleń: 672

Overloaded functions  More than one function of the same name, … but different parameters  Type of result is irrelevant (may not be used to overload) … because the result may be ignored in the call int f(int, int); int f(int); int f(long);  Pointers to overloaded functions - the compiler has to ...

Problems with References

  • Politechnika Śląska
  • Computer Programming
Pobrań: 0
Wyświetleń: 525

Problems with References  Problems: - when the actual parameter type is not the same as formal - … or is an expression: char c=1, int a=2, int b=3; swap(a, c); swap(a+1, b);  arguments are converted to const (const int), compiler expects int (not const)  it should exit with error, usually issues...