C++ 1

  • Uploaded by: sherwingodgift
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View C++ 1 as PDF for free.

More details

  • Words: 1,349
  • Pages: 25
Why C++

Objectives In this lesson, you will learn to: • • • • • •

History of C++ Features of C++ Sample C++ program Data types Variables Conditional and looping statement

h Invent

Why C++

History of C++ • 1980:Bjarne Stroustrup of AT&T Bell Laboratories added the concept of classes of C. The language is called “C with classes” • 1983: “C with classes” is called C++ • 1985:AT&T begins Commercial distribution of C++ • 1986:Cfront-apreprocessor that translates C++ to C is widely available. The C++ Programming Language by Stroustrup is published • 1988: The first true C++ compiler becomes available

h

Invent

Why C++ 1989: Release 2.0 becomes widely available 1990:ANSI standardization of C++ begins. The Annotated C++ Reference Manual by Stroustrup is published Features Inherited From C • Built-in types (float,int, etc.), operators, expressions. • Syntax for pointers, arrays, structures, const, etc. • Syntax for functions (C prototypes from C++). • Syntax for decisions and control.

h Invent

Why C++ Features Inherited From C (Contd…..) • File organization: header (.h), source (.c, .cc, .C, .cpp, .cxx, etc.). • Preprocessor (but used less). • External linkage model. • Standard conversions (including narrowing). • Efficiency: You pay only for what you use. Features of C++ • Data Abstraction • Reusability • Reliability

h Invent

Why C++

Features Inherited From C++(contd…..) • Extensibility OOPS Concepts • Class and Object • Abstraction • Encapsulation • Inheritance • Polymorphism etc.

h Invent

Why C++

A sample C++ program #include //preprocessor directive int main() //main function { //Opening Bracket //our program code goes here cout<<“My First C++ Program”; } //Closing Bracket

h Invent

Why C++ • In the above program #include Is knows as preprocessor directive. • The preprocessor directive attaches the code of the header file to your program.( These files contain all information that is required to make use of c++ libraries) • int is a C++ language keyword. Keywords are predefined names given special meaning within the language. int represents a built-in integer data type. (I have much more to say about data types in the next section.) • A function is an independent code sequence that performs some computation. It consists of four parts: thereturn type, the function name, the parameter list, and the function body. Let's briefly look at each part in turn.

h

Invent

Why C++ •

The return type of the function usually represents the result of the computation. main() has an integer return type. The value returned by main() indicates whether our program is successful. By convention, main() returns 0 to indicate success. A nonzero return value indicates something went wrong. • The name of a function is chosen by the programmer and ideally should give some sense of what the function does. • main is not a language keyword. The compilation system that executes our C++ programs, however, expects a main() function to be defined. If we forget to provide one, our program will not run.

h

Invent

Why C++ • The body of the function is enclosed in curly braces ({}). It holds the code sequence that provides the computation of the function. The double forward slash (//) represents a comment, a programmer's annotation on some aspect of the code. It is intended for readers of the program and is discarded during compilation. Everything following the double forward slash to the end of the line is treated as a comment.

h Invent

Why C++

• To write to the user's terminal, we use a predefined class object named cout .We direct the data we wish cout to write using the output operator <<. Example : cout <<“Enter Your Name :”; • To read input from the user's terminal, we use a predefined class object named cin We use the >> to direct cin to read data from the user's terminal into an object of the appropriate type. Example : cin>>cName ;

h Invent

Why C++

Built In Data Types in C++ Data type

Number of bytes on 32 bit computer

Range

char

1

-128 to 127

int

4

-231 to (231 )-1

float

4

6 Digits of precision

unsigned char

1

0 to (28)-1

unsigned int

4

0 to (232)-1

long int

4

-231 to 231 –1

unsigned long int

4

0 to (232)-1

short int

2

(215) to (215)-1

unsigned short int

2

0 to (216)-1

double

8

12 Digits of precision

long double

12

16 Digits of precision

bool

1

0 or 1

wchar_t

4

-231 to (231 )-1

h

Invent

Why C++ Defining Data Variables Data Definition

Data Type

Memory Defined

Size in bytes

value Assigned

char a,c;

char

a c

1 1

-

char a='z';

char

a

1

z

int count;

int

count

4

-

int a, count=10;

int

a count

4 4

10

float fnum;

float

fnum

4

-

float fnum1,fnum2=99. 62f; fnum2=99.62f;

float

fnum1 fnum2

4 4

99.62

h

Invent

Why C++

Variables Naming Conventions in C++ • Variable name must not have embedded space or symbols such as: ? ! @ # + - % ^ & * ( ) [ ] { } . , ; : “ ‘ / and \. We can use underscore(_). • Variable names are normally written in lowercase letters and can have any number of characters. • If a variable contains two or more worlds join the world and begin each word an uppercase letter or separate each world with a underscore. • A variable name start with any letter or an underscore but It can’t be a number(0-9).

h

Invent

Why C++ • Keywords can not be used as variable name. • Example address1 , employee_name , employeeName • Invalid variable name #phone, 1stName Operators in C++ • Comparison operator like ==,!=,>,<,>=and <= are present in C++. • It has arithmetic operators like +,-,*, / and % . • Logical operator supported by C++ are &&(and) , || (or) and !(not). • Assignment operators =, +=, -=,*=,/= and %= is supported by it.

h

Invent

Why C++

Conditional statements in C++ • Like in c, C++ also has only three conditional statement. – Switch case – If – Ternary operator (?:)

• Iteration statements in C++ • It has all C looping ststments – While – Do while – for

h Invent

Why C++

if statement if(contition) { // Executes if condition is true } else { // Executes if condition is false }

h Invent

Why C++ Nested if if (condition) { if (condition) { //statements }//close of inner if else { //statements }//close of inner else } //close of outer if else { //statements }//close of outer if

h Invent

Why C++ Switch case Switch (x) { case 1: cout<<“Option 1”; break; case 2: cout<<“Option 2”; break; case 3: cout<<“Option 3”; break; default : cout<<“Not a choice”; }

h Invent

Why C++

Ternary operator (?:) cout<< (a >b)? a : b; while loop while (condition) { //statements }

h Invent

Why C++ do while { //statements }while(expr); for loop for(initialzation_expr;test_expr;change_expr) { //statements }

h Invent

Why C++

Arrays Arrays in c and c++ are same Example: char name[20]; char str[]=“Sandy”; int amount[4]={4,5,7,2}; float sal[3]={2.4f,3.4f,2.4f}; These arrays can be accepted using for loops as in c language.

h

Invent

Why C++

Exercise • Write a program to find out the highest and lowest values from given 20 input from user. • Accept a String from the user and print it in reverse.

h Invent

Why C++ Double Dimensional array char arr[][5]={ “ant”, “BUGS” }; char arr[2][5]={ {‘a’,’n’,’t’,’\0’}, {‘B’,’U’,’G’,’S’,’\0’} }; We can also use nested for loops to accept and display the double dimensional arrys.

h

Invent

Why C++

Compilation of C++ program on linux • Save program with extension .cc or .cpp • Compile program as $ g++ prog1.c • Will produce a executable file a.out • Now you can execute a.out to see output of your program

h Invent

Why C++

Creation of user libraries Command $g++ -c prog1.cc prog2.cc Will generate prog1.o and prog2.o Adding .o files to user library $ar –r libuser.a prog1.o prog2.o Using user libary $g++ main.cc –L/home/user1 -luser

h Invent

Related Documents

C#-1
November 2019 15
C-1
October 2019 12
C-1
October 2019 15
C-1
October 2019 22
C++ 1
May 2020 19
C++ 1
June 2020 13

More Documents from "sherwingodgift"

C++ 1
June 2020 13
C++ 2
June 2020 8