0% found this document useful (0 votes)
73 views6 pages

What Is C++?

The document provides an overview of the C++ programming language, including its history, uses, advantages, and basic constructs. It describes how C++ began as an enhancement to C to support object-oriented programming. Key elements discussed include variables, operators, decisions structures like if/else and switch statements, and loop structures like for, while, do-while loops.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views6 pages

What Is C++?

The document provides an overview of the C++ programming language, including its history, uses, advantages, and basic constructs. It describes how C++ began as an enhancement to C to support object-oriented programming. Key elements discussed include variables, operators, decisions structures like if/else and switch statements, and loop structures like for, while, do-while loops.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C++

Page 1 of 6

C++
What is C++? Low Level Languages - Machine Code, Assembler Mid Level Languages - Java, C++, Pascal, C# High Level Languages - Visual Basic, Symantec Cafe, Delphi History of C++ (1958) Algol Created The first ever high-level structured language with a systematic syntax. (1969) UNIX created using BCPL (Basic Combined Programming Language) B created by Ken Thompson, as a replacement for BCPL. (1970) Pascal, a very formal and well defined language established as the successor to Algol (1973) C completed, and released as the successor to B, giving the user control of data types. (1979) Bjarne Stroustrup begins work on C-with-Classes, an Object Orientated version of C. (1983) C-with-Classes redesigned and released as C++ (1985) First mass release of C++ compilers. (1988) Lots of versions of C begin to emerge. (1989) ANSI/ISO standardisation of C begins (1999) and a decade later finally approved. (1995) Java goes public, viewed by many as being the successor to C++. (2002) Java, the language of the web, has taken over the world, and C++ is becoming a dead language.. .only it hasnt. What is C++ used for? Word Processors Operating systems Web Browsers Databases Spreadsheets, Mail Software. Games, Music Software. .basically everything Almost everything you see is written using Visual C++. Even Visual C++ was written in C++. So why all this Java stuff then? Java is an important language. It's easy to learn. Its portable. It removed multiple errors. It removed complications like templates and pointers. Added garbage collection. Added multithreading. Protected the user from making errors. Some see the Recipe for Java as: Take C++. Subtract functionality, speed and power. Box stuff in classes that take up 10 times the space that they have to. Allocate everything on the heap Add bugs to every implementation

C++ Compilers vs Interpreters

Page 2 of 6

Compiled: Runs faster Typically has more functionality Easier to Optimise More instructions available Best choice for complex programs that need to be fast Interpreted: Slower, but often easier to develop Allows runtime Flexibility More appropriate for use on the web

The Java Model Java is unique because it is both compiled and interpreted. Java is compiled into byte code, and then interpreted by the virtual machine. Great Advantage of Java is that the code can be compiled once and then run on any other type of machine. Except it cant necessarily, because of variability among java interpreters.

C++ Disadvantages It does not protect you from making mistakes. The power it offers the programmer makes it very easy to crash your PC. C++ with its many concepts and possibilities has a steep learning curve. extensive use of operator overloading, function overloading and virtual functions can very quickly make C++ programs very complicated. Shortcuts that it offers can often make it completely unreadable. C++ Advantages C++ is very powerful. It is very efficient. It is extremely fast. Everything can be written using C++. As you learn C++, you learn how computers work. Once you learn C++, you will not want to program in Java. C++ files Traditionally C programs use the file extension .C and C++ programs the extension .CPP C is essentially a subset of C++, so you could use a C++ compiler to run a C program. The two languages are extremely similar. In the labs we will be using a unix based compiler called g++. As ever, because its on unix its not the most user friendly. I recommend Turbo C++ by borland to install on your own PC.

C++ Anatomy of a Program The project source for a C++ program: #include <stdio.h>; int main() { int x = 3; cout << x << people left\n; cout << Darius. Stop. Please.; }

Page 3 of 6

Very Little to Learn #include is the preprocessor command used, to allow the program access to more functions, in this case the cout command, from the C++ standard Liibraries This is almost always used because the core C++ language has hardly any reserved words: auto break case char default unsigned void do double else enum extern const continue goto if int long register return float for sizeof static struct switch typedef union short signed volatile while

Initial Similarities C++ is strongly typed. C++ is Object Orientated. C++ uses the same variable types. C++ and Java have the same rules for Identifier Types: The Good The Bad count 1count SClub7 HearSay ISS_pro ISS pro Comments in Code Comments are also identical to Java. // this is a comment on one line /* this is a comment and it is on more than one line */ This is difference to C, where only the second type of comment is allowed. C++ Techniques 1. Variables 2. Operators 3. Decisions 4. Loops

C++ 1. C++ Variable Types There are various variable types available: DataType char int longInt float double boolean 2. C++ Operators Operators are similar to in Java: Addition Subtraction Multiplication Real number division Integer division Logical AND Logical OR Logical NOT More Identical operators: Equal to Not equal to Less than Greater than Less than / equal to Greater than / equal to The increment Operator Increment Decrement Shorthand Assigns Possible Range of Values -127 to 127 -32,767 to 32,767 -2,147,483,648 to 2,147,483,647 6 Digits of Precision 10 Digits of Precision True or False

Page 4 of 6

x = y + z; x = y z; x = y * z; x = y / 3.14; x = y / 10; if (x==1 && y==2) if (x==1 || y==2) if (!x) if (x==10) if (x!=10) if (x<10) if (x>10) if (x<=10) if (x>=10)

x++ or ++x x- or -x x*=3 (multiply x by 3) x+=5 (add 5 to x) x-=10 (subtract 10 from 6) x/=2 (halve x) There is a subtle difference between x++ and ++x. ++x will increment the variable before it does anything else with it, x++ will increment after any assignments.

In the first two cases x is set to 11, but in the first this is done before the assignment.

C++

Page 5 of 6

Simple Type Conversions When variable of one type are mixed with another Type Conversion occurs. This is not impossible in Java (where casting is needed), and would cause an error.

Different Outlooks This highlights the differing outlooks of the languages: C++ Assumes youre Clever. Java Assumes youre a desk monkey. Casting You can force an expression to be of a specific type by using casting. The general form of a cast is: (type) expression Where type is a valid data type. For example to make sure that the expression x/2 evaluates to type float, write: (float)x/2 Without the cast only an integer division would have been performed. 3. Decisions The structure of an if statement in C++ is as follows: Single statements: if (condition) true_statement; else false_statement; Multiple statements: if (condition) { } else { }

Nested If Statements void main() { int winner = 1; cout << and the winner of pop idol is ; if (winner==1) { cout << Gareth; } else if (winner==1) { cout << Will; } else if (winner==2) { cout << Darius; } }

C++ Switch Statements Switch statements look like this example: switch (expression) { value_1 : statements_1; break; value_2 : statements_2; break; ... value_n : statements_n; break; default { } } 5. Loops The syntax of the For loop in C++ is identical to Java: for (initialisation; condition; increment) { statements; } For example: for {int x = 1; x < 100; x++) { cout << Loop << x; } Special Cases Loops with no bodies, such as a time delay: for (time=0; time <10000; time++); You can have as many control variables as you want in loops. The following is fine: for (x=0, y=0; x+y<10; x++, y++) ... Infinite Loops for ( ; ; ) { cout << Alex Ferguson. Stop. Please; } The While Loop An example while loop looks like this: void main() { char ch; while (ch != Q) { ch = getchar(); } }

Page 6 of 6

The Do-while Loop The do-while loop repeatedly executes a block of code indicated by statements as long as the conditional expression cond_expr is true. do { someBoringCalculation; } while (cond_expr); Continue & Break The continue statement is used to force program execution to the bottom of the loop, skipping any statements that come after it. The break statement is used to halt execution of a loop prior to the loops normal test condition being met. The exit statement causes the whole program to terminate if it is called within the main program block.

You might also like