1 / 22

OBJECT ORIENTED PROGRAMMING IN C++

OBJECT ORIENTED PROGRAMMING IN C++. LECTURE 03. What is a Variable?. Variables may be the most fundamental aspect of any computer language. A variable can be define as: A variable represents one or more storage location where program data is held. OR

Download Presentation

OBJECT ORIENTED PROGRAMMING IN C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OBJECT ORIENTED PROGRAMMING IN C++ LECTURE 03

  2. What is a Variable? • Variables may be the most fundamental aspect of any computer language. • A variable can be define as: A variable represents one or more storage location where program data is held. OR A variable is a place to store a piece of information. OR A variable is a space in the computer’s memory for a certain kind of data and given a name for easy reference.

  3. Variable Name or identifier • Each variable needs an identifier that distinguishes it from the others. • A valid identifier is a sequence of one or more letters, digits or underscore characters (_). Neither spaces nor punctuation marks or symbols can be part of an identifier. Only letters, digits and single underscore characters are valid. In addition, variable identifiers always have to begin with a letter. They can also begin with an underline character (_ ). • Another rule is that Keyword cannot be used as an identifier name and upper case and lower case are distinct. • Length: Variables names can contain any combination of characters as opposed to the restriction of 31 letters in C++.

  4. Fundamental data types • Data type means what type of data or information that are stored in a variable. • When programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them. • By defining data type, computer are easily identify:- • How many bytes are occupy in memory by variable. • What is the max and min value/range that we are able to store in variable. • and, what operations can apply on value and how complier can solve them. In next slide, there is a summary of the basic fundamental data types in C++, as well as the range of values that can be represented with each one:

  5. TYPE BOOL • Type bool gets its name from George Boole, a19th century English mathematician who invented the concept of using logical operators with true-or-false values. Thus such true/false values are often called Boolean values. • Variable of type bool can have only two possible values: true and false. • Type bool variable is most commonly used to hold the result of comparisons.

  6. Declaration of variables • In order to use a variable in C++, we must first declare it specifying which data type we want it to be. • In c++, we are free to declare any variable at any place in program. • The syntax for declararing a variable in c++ is same as c language: datatype variable-name; Examples: int age; float P1,P2; to declare unsigned variables, we must add keyword unsigned before datatype e.g: unsigned int radius; unsigned long l;

  7. Initialization of variables • When declaring a regular local variable, we may want a variable to store a concrete value at the same moment that it is declared. In order to do that, you can initialize the variable. There are two ways to do this in C++:The initialization, is done by appending an equal sign followed by the value to which the variable will be initialized:type identifier = initial_value ;For example, if we want to declare an int variable called a initialized with a value of 0 at the moment in which it is declared, we could write:int a = 0;

  8. #include <iostream> • void main () • { • int a=5; // initial value = 5 • int b=2; // initial value = 2 • int result; // initial value undetermined • a = a + 3; • result = a - b; • cout << result; • getch(); • }

  9. Scope of variables • A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block.

  10. Lecturer 04 C++ Operators

  11. CONSTANTS Lecture 05

  12. Constants • Constants are expressions with a fixed value. • Literals • Literals are used to express particular values within the source code of a program. • For example, when we wrote: a = 5; • the 5 in this piece of code was a literal constant. • Literal constants can be divided in • Integer Numerals, • Floating-Point Numerals, • Characters, Strings and • Boolean Values.

  13. Integer Numerals • 1776 • 707 • -273 • They are numerical constants that identify integer decimal values. • In addition to decimal numbers, C++ allows the use as literal constants of octal numbers (base 8) and hexadecimal numbers (base 16). • If we want to express an octal number we have to precede it with a 0 (zero character). And in order to express a hexadecimal number we have to precede it with the characters 0x (zero, x). For example, the following literal constants are all equivalent to each other: • 75 // decimal • 0113 // octal • 0x4b // hexadecimal • All of these represent the same number: 75 (seventy-five)

  14. Literal constants, like variables, are considered to have a specific data type. • By default, integer literals are of type int. However, we can force them to either be unsigned by appending the u character to it, or long by appending l: • 75 // int • 75u // unsigned int • 75l // long • 75ul // unsigned long • In both cases, the suffix can be specified using either upper or lowercase letters.

  15. Floating Point Numbers • They express numbers with decimals and/or exponents. They can include either a decimal point, an “e” character (that expresses "by ten at the Xth height", where X is an integer value that follows the e character), or both a decimal point and an e character: • 3.14159 // 3.14159 • 6.02e23 // 6.02 x 10^23 • 1.6e-19 // 1.6 x 10^-19 • 3.0 // 3.0 • These are four valid numbers with decimals expressed in C++. • If you explicitly want to express a float or long double numerical literal, we can use the f or l suffixes respectively: • 3.14159L // long double • 6.02e23f // float

  16. Character and string literals • There also exist non-numerical constants, like: 'z' 'p' "Hello world" "How do you do?" • The first two expressions represent single character constants, and the following two represent string literals composed of several characters. • Notice that to represent a single character we enclose it between single quotes (') and to express a string (which generally consists of more than one character) we enclose it between double quotes (").

  17. Boolean literals • There are only two valid Boolean values: true and false. These can be expressed in C++ as values of type bool by using the Boolean literals true and false.

  18. Program Example OUTPUT • #include<iostream.h> • #include<conio.h> • void main(void) • { • int i=10; • int a=0113; • int b=0x4b; • unsigned long x=7ul; • float y=2.3e20f; • clrscr(); • cout<<i+a+b; • cout<<“\n”<<i+x; • cout<<“\n”<<y; • getch(); • } 160 17 2.3 e+20

  19. THE const Qualifier: • The keyword const specifies that the value of a variable will not change throughout the program. • Any attempt to alter the value of a variable define with this qualifier will elicit an error message from the complier. • Example: • void main() • { • float rad; • const float PI=3.14159; • cout<<"Enter radius of circle: "; • cin>>rad; • float area=PI*rad*rad; • cout<<"Area is "<<area; • getch(); • }

  20. The #define Directive: • The #define directive tells the complier to substitute one thing for another. The directive sets up an equivalence between an identifier and a text phrase. • For example, the line • #define PI= 3.14159 • specifies that the identifier PI will be replaced by the text 3.14159 throughout the program.

  21. Examples • #include<iostream.h> • #include<conio.h> • #define PI 3.14159 • void main() • { • float rad; • clrscr(); • cout<<"Enter radius of circle: "; • cin>>rad; • float area=PI*rad*rad; • cout<<"Area is "<<area; • getch(); • }

More Related