Vous êtes sur la page 1sur 6

ASSIGNMENT : C++ PROGRAMMING

QUESTIONS
1. WHO IS WRITTEN C++

Bjarne Stroustrup, a Danish computer scientist, began his work on C++'s predecessor "C with Classes" in
1979. The motivation for creating a new language originated from Stroustrup's experience in programming for his
Ph.D. thesis. Stroustrup found that Simula had features that were very helpful for large software development,
but the language was too slow for practical use, while BCPL was fast but too low-level to be suitable for large
software development. When Stroustrup started working in AT&T Bell Labs, he had the problem of analyzing
the UNIX kernel with respect to distributed computing. Remembering his Ph.D. experience, Stroustrup set out to
enhance the C language with Simula-like features.] C was chosen because it was general-purpose, fast, portable
and widely used. As well as C and Simula's influences, other languages also influenced C++, including ALGOL
68, Ada, CLU and ML.
Initially, the class, derived class, strong typing, inlining and default argument features were added to C via
Stroustrup's "C with Classes" to C compiler, Cpre.
In 1983, it was renamed from C with Classes to C++ ("++" being the increment operater in C). New features were
added including virtual functions, function name and operator overloading, references, constants, type-safe freestore memory allocation (new/delete), improved type checking, and BCPL style single-line comments with two
forward slashes , as well as the development of a proper compiler for C++, Cfront.
In 1985, the first edition of The C++ Programming Language was released, which became the definitive
reference for the language, as there was not yet an official standard.The first commercial implementation of C++
was released in October of the same year.
In 1989, C++ 2.0 was released, followed by the updated second edition of The C++ Programming Language in
1991.New features in 2.0 included multiple inheritance, abstract classes, static member functions, const member
functions, and protected members. In 1990, The Annotated C++ Reference Manual was published. This work
became the basis for the future standard. Later feature additions included templates, exceptions, namespaces,
new casts, and a boolean type.

After the 2.0 update, C++ evolved relatively slowly. In 2011, the C++11 standard was released, adding numerous
new features, enlarging the standard library further, and providing more facilities to C++ programmers. After a
minor C++14 update, released in December 2014, various new additions are planned for 2017.

2. STATE STATEMENTS BELOW AND GIVE AN EXAMPLE APPLICATION IN C++


PROGRAM.
a) GO TO
/* C++ program to demonstrate the working of goto statement. */
/* This program calculates the average of numbers entered by user. */
/* If user enters negative number, it ignores that number and
calculates the average of number entered before it.*/
# include <iostream>
using namespace std;
int main() {
float num, average, sum = 0.0;
int i, n;
cout<<"Maximum number of inputs: ";
cin>>n;
for(i=1; i <= n; ++i) {
cout<<"Enter n"<<i<<": ";
cin>>num;

if(num < 0.0) {


goto jump; /* Control of the program moves to jump; */
}
sum += num;

jump:
average=sum/(i-1);
cout<<"\nAverage = "<<average;
return 0;
}
b) WHILE

The while construct consists of a block of code and a condition / expression . The
condition/expression is evaluated, and if the condition/expression is true , the
code within the block is executed. This repeats until the condition/expression
becomes false. Because the while loop checks the condition/expression before
the block is executed, the control structure is often also known as a pre-test
loop. Compare this with the do while loop, which tests the
condition/expression after the loop has executed.

while (true)
{
//do complicated stuf
if (someCondition) break;

//more stuf
}

c) BREAK AND CONTINUE


In C programming, break is used in terminating the loop immediately after
it is encountered. The break statement is used with conditional if
statement.
/* C program to demonstrate the working of break statement by terminating a
loop, if user inputs negative number*/
# include <stdio.h>
int main(){
float num,average,sum;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)
break;
//for loop breaks if num<0.0
sum=sum+num;
}
average=sum/(i-1);
printf("Average=%.2f",average);
return 0;
}

It is sometimes desirable to skip some statements inside the loop. In such


cases, continue statements are used.
//program to demonstrate the working of continue statement in C programming
# include <stdio.h>
int main(){
int i,num,product;
for(i=1,product=1;i<=4;++i){
printf("Enter num%d:",i);
scanf("%d",&num);
if(num==0)
continue; / *In this program, when num equals to zero, it skips the
statement product*=num and continue the loop. */
product*=num;
}
printf("product=%d",product);
return 0;
}

d) WHILE TRUE

I'm curious about using a while statement with a true condition.


What is the advantage of using a while(true) statement with break
to exit the while statement over something like a for loop? I guess
I'm just curious when a good time to use this technique would be? I
saw it demonstrated in a quick sort algorithm at school today and
was just curious about it.

#include <iostream>
using namespace std;
int main (int argc, char * const argv[]) {
int counter = 9;
while (true) {
cout << "Counter: " << counter << endl;;
counter--;
if (counter == 0) {
break;
}
}
return 0;
}

e) DO/WHILE

The do-while statement can also terminate when a break, goto,


or return statement is executed within the statement body.
#include <iostream>
int main()
{
// selection must be declared outside do/while loop
int selection;
do
{
std::cout << "Please make a selection: \n";
std::cout << "1) Addition\n";
std::cout << "2) Subtraction\n";
std::cout << "3) Multiplication\n";
std::cout << "4) Division\n";
std::cin >> selection;
}
while (selection != 1 && selection != 2 &&
selection != 3 && selection != 4);
// do something with selection here
// such as a switch statement
std::cout << "You selected option #" << selection << "\n";
return 0;
}

f) JUMP/LOOP
The Loop jump is a figure skating jump that takes off from a back
outside edge and lands on the same backwards outside edge. For a
jump with counterclockwise rotation, this is the right back outside
edge. It is named from its similarity to the loop compulsory figure.
The invention is widely credited to Werner Rittberger, and the jump
is also known as the "Rittberger" in Europe. However, evidence
exists that it may have been first done as early as the 1880s.

g) IF/ELSE
An if statement can be followed by an optional else statement, which
executes when the boolean expression is false.

if(condition)
{
// Block For Condition Success
}
else
{
// Block For Condition Fail
}
#include <iostream>
using namespace std;

int main ()
{
// local variable declaration:
int a = 100;

// check the boolean condition


if( a < 20 )
{
// if condition is true then print the following
cout << "a is less than 20;" << endl;
}
else
{
// if condition is false then print the following
cout << "a is not less than 20;" << endl;
}
cout << "value of a is : " << a << endl;

return 0;
}

Vous aimerez peut-être aussi