Yasmin Sami
Yasmin Sami
Yasmin Sami
Engineering Faculty
Computer Engineering Department
Graphic User Interface (GUI) based applications: Many highly used applications, such as Image
Ready, Adobe Premier, Photoshop and Illustrator, are scripted in C++.[4]
Games: C++ overrides the complexities of 3D games, optimizes resource management and
facilitates multiplayer with networking. The language is extremely fast, allows procedural
programming for CPU intensive functions and provides greater control over hardware, because of
which it has been widely used in development of gaming engines. For instance, the science fiction
game Doom 3 is cited as an example of a game that used C++ well and the Unreal Engine, a suite
of game development tools, is written in C++.[4]
Medical and Engineering Applications: Many advanced medical equipments, such as MRI machines,
use C++ language for scripting their software. It is also part of engineering applications, such as
high-end CAD/CAM systems.[4]
[7]
III.I. Introduction.
A data structure is a group of data elements grouped together under one name. These data elements,
known as members, can have different types and different lengths.
Simple Example
struct product {
int weight;
double price;
};
product apple;
product banana, melon;[10]
#include<iostream>
using nam()
{
char mychar="A";
cout<<mychar;
}
[12]
3. Enumeration Type.
An enumeration is a user-defined data type that consists of integral constants. To define an
enumeration, keyword enum is used.
Example
#include <iostream>
using namespace std;
enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3
} card;
int main()
{
card = club;
cout << "Size of enum variable " << sizeof(card) << " bytes.";
return 0;
}[13]
4. Array Type.
C++ provides a data structure, the array, which stores a fixed-size sequential collection of
elements of the same type. An array is used to store a collection of data, but it is often more
useful to think of an array as a collection of variables of the same type.
Array are important to C++ and should need lots of more detail.There are following
few important concepts,which should be clear to a C++ programmer
Multi-dimensional array
Pointer to an array
Passing arrays to function
Return arrays to function
#include<iostream.h>
void main()
{
int a[4][4],i,j,s=0;
cout<<"Enter the elements of the matrix"<<endl;
for(i=0; i<4; i++)
for(j=0; j<4; j++)
cin>>a[i][j];
}[14]
5. Associative Arrays.
Associative arrays are also called map or dictionaries. In C++. These are special kind of
arrays, where indexing can be numeric or any other data type i.e can be numeric 0, 1, 2,
3.. OR character a, b, c, d… OR string geek, computers…
These indexes are referred as keys and data stored at that position is called
value.So in associative array we have (key, value) pair.
We use STL maps to implement the concept of associative arrays in C++.
Example
int[string] aa;
aa["hello"] = 3;
int value = aa["hello"];
assert(value == 3);
[15]
6. Record Types.
Records are used to group related component of different types. Component of the record are
called fields.
In C++
Record called a struct(structure)
Fields called members
FieldName - the identifier used to refer to the field. The identifier only need to be
unique within the struct. It will not be in conflict with names usedlsewhere or in
other structs.Records have been implemented as classes in C++.
struct StudentType {
int idNumber;
char name[30];
StatusType status;
float credits;
float gpa;
};
StudentType student1;
StudentType student2;
[16]
7. Tuple Types.
A tuple is an object capable to hold a collection of elements. Each element can be of a
different type. A list of types used for the elements, in the same order as they are going to
be ordered in the tuple.
Example
#include <iostream>
#include <tuple>
int main ()
{
auto mytuple = std::make_tuple (10,'a');
std::tuple_element<0,decltype(mytuple)>::type first = std::get<0>(mytuple);
std::tuple_element<1,decltype(mytuple)>::type second = std::get<1>(mytuple);
std::cout << "mytuple contains: " << first << " and " << second << '\n';
return 0;
}[17]
8. List Types.
Table 3:Type of linked list.
linked list Double-linked list
Lists are sequence containers that allow Doubly linked lists can store each of the
constant time insert and erase operations elements they contain in different and
anywhere within the sequence, and unrelated storage locations. The ordering
iteration in both directions. is kept internally by the association to
each element of a link to the element
preceding it and a link to the element
following it.
.[18]
9. Union Types .
A union is a special data type where all members start at the same address. A union can hold
only one type at a time; therefore, you can save memory. A tagged union is a union which
keeps track of its types.
Use unions to save memory.
Avoid “naked”unions.
Use anonymous unions to implement tagged unions.
Don’t use a union for type punning.
Example
union Value {
int i;
double d;
};
Value v = { 123 };
cout << v.i << '\n';
v.d = 987.654;
cout << v.d << '\n'; This example is use union to save memory.[19]
10.Pointer and Reference Types.
Pointers, References and Dynamic Memory Allocation are the most powerful features in C/C++
language, which allows programmers to directly manipulate memory to efficiently manage the
memory - the most critical and scarce resource in computer - for best performance. However,
"pointer" is also the most complex and difficult feature in C/C++ language.
#include <iostream>
using namespace std;
int main() {
int number = 88; // Declare an int variable and assign an initial value
int * pNumber; // Declare a pointer variable pointing to an int (or int pointer)
pNumber = &number; // assign the address of the variable number to pointer pNumber
cout << pNumber << endl;
cout << &number << endl;
cout << *pNumber << endl;
cout << number << endl;
*pNumber = 99;
cout << pNumber << endl;
cout << &number << endl;
cout << *pNumber << endl;
cout << number << endl;
cout << &pNumber << endl;
}[20]
III.III.Expressions and Assignment Statements
1) Variable definition
A variable definition tells the compiler where and how much storage to create for the
variable. A variable definition specifies a data type, and contains a list of one or more
variables.
Figure 2: scope of variable.
Simple example:
//variable definition
int a,b;
double k;
//variable initializarion
a=10;
k=8.67;
[21]
2) Arithmetic expressions
Most programs perform arithmetic calculations. Figure 2. Summarizes the C++ arithmetic
operators. Note the use of various special symbols not used in algebra. The asterisk (*) indicates
multiplication and the percent sign (%) is the modulus operator that will be discussed shortly. The
arithmetic operators in Figure. 2are all binary operators, i.e., operators that take two operands. For
example, the expression number1 + number2 contains the binary operator + and the two
operands number1 and number2.
Simple example:
Algebraic y=mx+b
C++ y = m * x + b;
Addition + f+7 f + 7
Subtraction - p–c p - c
Multiplication * bm or b · m b * m
Division / x / y
x / y or or x ÷ y
Modulus % r mod s r % s
[22]
3)Overloaded operators
C++ allows you to specify more than one definition for a function name or an operator in
the same scope, which is called function overloading and operator
overloading respectively.
4) Type conversions
A conversion produces a new value of some type from a value of a different type. Standard
conversions are built into the C++ language and support its built-in types, and you can create user-
defined conversions to perform conversions to, from, or between user-defined types.
The standard conversions perform conversions between built-in types, between pointers or
references to types related by inheritance, to and from void pointers, and to the null pointer. For
more information, see Standard Conversions. User-defined conversions perform conversions
between user-defined types, or between user-defined types and built-in types. You can implement
them as Conversion constructors or as Conversion functions.
#include <iostream>
class Money
{
public:
Money() : amount{ 0.0 } {};
Money(double _amount) : amount{ _amount } {};
double amount;
};
void display_balance(const Money balance)
{
std::cout << "The balance is: " << balance.amount << std::endl;
}
int main(int argc, char* argv[])
{
Money payable{ 79.99 };
display_balance(payable);
display_balance(49.95);
display_balance(9.99f);
return 0;
} [24]
5) Relational and Boolean Expressions
Relational operators are used to compere value. All the expressions evaluates from left to
right.
Taible 1. List of operator in C++
Symbols meaning Example
< Less than (A>B) is true
<= Less than or equal (A<=B) is not true
> Greater than (A>B) is true
>= Greater than or equal (A>=B) is not true
== equal (A==B) is not true
!= Not equal (A!=B) is true
All operators evaluates results true or false. False statemant represent by 0 ,true statement
represent by 1.
Example
int a=5;
if(a<=7)
{
cout<<"true";
}
else
cout<<"false ";[25]
6) Assignment statement
It is essential that every variable in a program is given a value explicitly before any attempt is
made to use it. It is also very important that the value assigned is of the correct type.
The most common form of statement in a program uses the
Assignment operator
Expression or Constant assignment
variable = expression;
variable = constant;
The assignment statement:
total=total+5;
is thus a valid statement since the new value of total becomes the old value of total with 5 added
to it. Remember the assignment operator (=) is not the same as the equality operator in
mathematics (represented in C++ by the operator ==).[26]
[27]
2) Ierative starement
Directs computer to repeat one or more instruction until some condition met.
Also called loop or iteration
for loop
while loop
do…while loop
for loop example While loop example Do…while example
for(int i=1 ; i<10 ; i++) int i=1; int i=1;
{ While( i<10) do{
Cout<< i ; { cout<< i ;
} Cout<< i ;} }while(i<10);[27]
References
2. History: How it started, who defined the language, how became popular.[online] A valiable at:
http://www.cplusplus.com/info/history/