C tricks that you need to know to understand the codes in the textbook - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

C tricks that you need to know to understand the codes in the textbook

Description:

C tricks that you need to know to understand the codes in the textbook ... Tricks that you need to know to understand the codes in the textbook. 17. Friends ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 21
Provided by: math81
Category:

less

Transcript and Presenter's Notes

Title: C tricks that you need to know to understand the codes in the textbook


1
C tricks that you need to know to understand
the codes in the textbook
2
explicit keyword
class Square public Square(const double
size) lengthsize void setLength(const
double size) lengthsize double area()
const return lengthlength bool
compareArea(const Square otherSquare) const
if (lengthlength lt otherSquare.area()) return
true else return false private
double length
3
explicit keyword
  • Main Program 1
  • void main()
  • Square sqA(10) Square sqB(15)
  • if (sqA.compareArea(sqB))
  • cout ltlt sqA is smaller than sqB.\n
  • else
  • cout ltlt sqA is not smaller than sqB.\n
  • Main Program 2
  • void main()
  • Square sqA(10)
  • if (sqA.compareArea(25.0))
  • cout ltlt sqA is smaller than sqB.\n
  • else
  • cout ltlt sqA is not smaller than sqB.\n

Output sqA is smaller than sqB.
Output ??
4
explicit keyword
  • What is the output of main program 2? Lets have
    a vote!
  • Program cant compile.
  • sqA is smaller than sqB.
  • sqA is not smaller than sqB.
  • None of the above.

5
explicit keyword
  • The output of main program B is B.
  • SquarecompareArea requires one parameter of
    type Square. However, in the main program B,
    the type of the input parameter is double.
  • So, we would expect the program cant compile.
  • No. C is very kind and try to help us. He
    searches for constructor(s) in Square class to
    convert a double variable to a Square object.
  • Luckily (??), C finds
  • SquareSquare(const double size)
    lengthsize
  • which converts 25.0 to a Square object (which
    has length 25.0).
  • So the output of the program is

sqA is smaller than sqB.
6
explicit keyword
  • To avoid the implicit conversion by C, we add a
    keyword explicit before the constructor.
  • explicit Square(const double size)
    lengthsize
  • Now, the following cant compile
  • sqA.compareArea(25.0)
  • We only need to add explicit keyword to
    constructors which have a single parameter.

Square.cpp30 no matching function for call to
SquarecompareArea (double)' Square.cpp10
candidates are bool SquarecompareArea(const
Square ) const
7
Function parameters default values
  • The parameters of a function can be assigned to
    default values.
  • void print(int x10)
  • cout ltlt x ltlt endl
  • print()
  • print(15)

Output 10
Output 15
8
Function parameters default values
  • We may assign more than one default values to the
    function parameters.
  • void print(int x10, int y100)
  • cout ltlt x ltlt \t ltlt y ltlt endl
  • print()
  • print(-1)
  • print(15, 150)

Output 10 100
Output -1 100
Output 15 150
9
Function parameters default values
  • We may want to assign default values to some of
    the function parameters. The default parameters
    must be placed at the end of the parameter list.
  • void print(int x, int y100)
  • cout ltlt x ltlt \t ltlt y ltlt endl
  • print()
  • print(15)
  • print(-2, 150)

//too few arguments to function void print(int,
int 100)
Output 15 100
Output -2 150
10
Constructors initializer list
  • class Square
  • public
  • Square(const double size, const string
    colr)
  • length(size), color(colr)
  • //
  • private
  • double length
  • string color
  • Square sqA(10, blue)
  • The sqA object is initialized with length10, and
    colorblue.

11
Constructors default initialization values and
initializer list
class Square public Square(const double
size, const string colrblack)
length(size), color(colr) // private
double length string color Square
sqA(10) Square sqB(15, blue) Square
sqC(red)
// sqA is initialized with length10, and
colorblack.
// sqB is initialized with length15, and
colorblue.
// program cant compile.
12
Friends
  • C provides information hiding, i.e., if data
    members (or member functions) are declared as
    private, then we can not accessed them directly.
  • We can override this by declaring other class (or
    function) as our friend.

13
Friends
  • Lets get back to our game example.
  • In our game, there are two types of object,
    Soldier and Kingdom.
  • class Kingdom
  • public
  • // upgrade the footman armor if the footmans
    HP gt250
  • void updateFootmanArmor()
  • private
  • Soldier footmanTroop100

14
Friends
  • Here is the suggested codes
  • // upgrade the footman armor if the footmans HP
    gt250
  • void KingdomupdateFootmanArmor()
  • int i
  • for (i0 ilt99 i)
  • if (footmanTroopi.HP gt250)
  • footmanTroopi.ArmValue2

15
Friends
  • This function cant compile, since it tries to
    access the private data (HP, ArmValue) of the
    Soldier objects.

Kingdom.cpp In method void KingdomupdateFootma
nArmor()' Kingdom.cpp18 double SoldierHP'
is private Kingdom.cpp50 within this
context Kingdom.cpp18 double
SoldierArmValue' is private Kingdom.cpp51
within this context
16
Friends
  • One way to solve this problem is to make HP, and
    ArmValue public in Soldier class. However, this
    is a bad solution.
  • The other solution is to declare Kingdom class as
    a friend of Soldier class.
  • By doing so, a Kingdom object can access to the
    private data members (functions) of Soldier
    class.
  • But, Solider is NOT a friend of Kingdom. A
    Soldier object cant access to the private
    section of a Kingdom object.

17
Friends
  • Class Kingdom
  • Class Soldier
  • // details omitted
  • private
  • double HP, AttValue, AttRate, ArmValue
  • // declare class Kingdom as a friend of class
    Soldier
  • friend class Kingdom

18
Templates
  • Template is a generic types.
  • template ltclass Tgt
  • class Storage
  • T _store100
  • public
  • Storage() // default constructor
  • void placeAt(const int i, const T x)
  • _storeix
  • Storage is a template class a template class is
    not an actual class, it is a pattern for a class
    that can be created from it. Here, Storage needs
    a parameter T (which is a class) to create an
    actual class.

19
Templates
  • Example A
  • Storageltintgt myIntstorage
  • myIntstorage.placeAt(50, 120)
  • myIntstorage.placeAt(51, COMP) // cant
    compile
  • Example B
  • Storageltstringgt myStringstorage
  • myStringstorage.placeAt(50, "COMP 171")
  • myStringstorage.placeAt(51, "IS GOOD")
  • myStringstorage.placeAt(52, 100) // cant compile

// myIntstorage._store is an int array of size 100
// myIntstorage._store50120
20
The Big-Three Destructor, Copy Constructor, and
operator
  • We will discuss the big-three in the first
    tutorial.
  • Destructor and Copy Constructor are NOT new to
    you. So you should spend your weekend to read
    the materials that you have learnt in your
    previous C course.
  • In the first tutorial, we will also do some small
    exercises in C as well.
Write a Comment
User Comments (0)
About PowerShow.com