Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modifying Objects Assignment operation Assignment conversions

Similar presentations


Presentation on theme: "Modifying Objects Assignment operation Assignment conversions"— Presentation transcript:

1 Modifying Objects Assignment operation Assignment conversions
Assignment precedence and associativity Extraction operations CONST declarations Compound assignment operations Input with cin Increment and decrement operation strings 5/6/2019 Y K Choi

2 Memory Depiction float y = 12.5; memory

3 Memory Depiction float y = 12.5; int Temperature = 32;

4 Memory Depiction float y = 12.5; int Temperature = 32;
char Letter = 'c';

5 Memory Depiction float y = 12.5; int Temperature = 32;
char Letter = 'c'; int Number;

6 Assignment -- = The operator is =
The operator stores a value into an object Example: int x = 3; int i = 4; //4 bytes for this variable i float weight = 120.4; //floating point (4 bytes for single precision) unsigned int i = 4; //no negative (4 bytes) Note that it is better to define unsigned for looping such as for unsigned i = … as it is faster.

7 Assignment Conversions
#include <iostream> #include <string> using namespace std; void main() { short s1 = 0; long i2 = 65536; s1 = i2; cout<<"i2 is " << i2 <<endl; cout <<"s1 is " << s1 <<endl; } S1 is 0 instead of 65536, interesting, as 16 bits are reserved for s1, the highest 16 bits (16 out of 32) is chopped off.

8 Screen dump – note the value of s1

9 It means pi is always 3.1416 within the program.
CONST definition The CONST definition is: const Type Identifier = Expression; const float light_speed = 3.0e8; //the speed of light const float pi = ; //the pi It means pi is always within the program.

10 Input statements Cin (pronounced as C in) and operator >>
Example int x; //define i as an integer cin >> i; //get the value from keyboard and load into I char cvalue; //define cvalue as char cin >> cvalue; //load a single byte (8 bits) into cin

11 Screen dump

12 Note the operators used, it optimises the programs
Compound assignments int j = 7; int i = 5; int k = 3; i /=k; //i becomes i = i/k j *= i; // j = j *i j %= i; //j = j %i Note the operators used, it optimises the programs

13 Increment and Decrement
Operator ++ and – Example After the operation, i becomes 4 Before this operation, i becomes 4 int i = 3; ++i; cout <<“i is “ <<i <<endl; int i = 3; i++; cout <<“i is “ <<i <<endl; Both will display the values of 4.

14 The operation is j * (i +1)
How about j *i++; Screen dump The operation is j * (i +1)

15 Nonfundamental Types Nonfundamental as they are additions to the language C++ permits definition of new types and classes A class is a special kind of type Class objects typically have Data members that represent attributes and values Member functions for object inspection and manipulation Members are accessed using the selection operator (.) j = s.size(); Auxiliary functions for other behaviors Libraries often provide special-purpose types and classes Programmers can also define their own types and classes

16 Examples Standard Template Library (STL) provides class string
EzWindows library provides several graphical types and classes SimpleWindow is a class for creating and manipulating window objects RectangleShape is a class for creating and manipulating rectangle objects

17 Class string Class string Some definitions
Used to represent a sequence of characters as a single object Some definitions string Name = "Joanne"; string DecimalPoint = "."; string empty = ""; string copy = name; string Question = '?'; // illegal

18 Nonfundamental Types To access a library use a preprocessor directive to add its definitions to your program file #include <string> The using statement makes syntax less clumsy Without it std::string s = "Sharp"; std::string t = "Spiffy"; With it using namespace std; // std contains string string s = "Sharp"; string t = "Spiffy";

19 Screen dump

20 Summary Assignment conversions Assignment precedence and associativity
Extraction operations CONST declarations Compound assignment operations Input with cin >> x Increment and decrement operation ++ and -- Strings


Download ppt "Modifying Objects Assignment operation Assignment conversions"

Similar presentations


Ads by Google