17
Beginning with C++ Name: VINOD PILLAI Mobile: 9909023599 Email: [email protected]

Beginning with c++

Embed Size (px)

Citation preview

Page 1: Beginning with c++

Beginning with C++

Name: VINOD PILLAI

Mobile: 9909023599

Email: [email protected]

Page 2: Beginning with c++

C++

C++ is an object-oriented programminglanguage.

C++ was developed by Bjarne Stroustrup atAT&T Bell Laboratories in Murray Hill, NewJersey, USA.

C++ is an extension of C with a major addition ofthe class construct feature.

Since the class was a major addition to theoriginal C language Stroustrup called the newlanguage 'C with Classes'.

Page 3: Beginning with c++

C++

However later in 1983 the name was changed toC++.

The idea of C++ comes from the C incrementoperator ++ thereby suggesting that C++ is anincremented version of C

C++ is a superset of C.

The three most important facilities that C++adds on to C are classes, function overloading,and operator overloading.

Page 4: Beginning with c++

Simple C++ Program

#include<iostream>

using namespace std;

int main()‏

{

cout<<"Hello C++ Programmer";

return 0;

getch();

}

Page 5: Beginning with c++

C++

Above example contains only one fucntion,main().

Execution begins at main().

Every C++ program must have a main().

Page 6: Beginning with c++

Comments

// - (Single Line comment)‏

/*

*/ -( Multiline comment)‏

for(j=0;j<10;/* hello */ j++) //Multiline only allowed

{

}

Page 7: Beginning with c++

C++ Output Operator

The Statement:

cout<<”C++ is better than C”;

This causes the string in quotation marks to bedisplayed on the screen.

This statement introduces two new C++ features,

cout and <<.

cout: Is a predefined object that represents thestandard output stream in C++.

Here the standard output stream represents thescreen.

Page 8: Beginning with c++

C++ Output Operator

It is also possible to redirect the output to otheroutput devices.

<< : Is called the insertion or put to operator.

It inserts the contents to the variable on its right tothe object on its left.

It is important to note that we can still use theprintf() for displaying an output.

You may recall that the operator << is the bit-wiseleft-shift operator and it can still be used for thispurpose. (Operator Overloading)

Page 9: Beginning with c++

C++ iostream File

#include <iostream>

This directive causes the preprocessor to add thecontents of the iostream file to the program.

It contains declarations for the identifier cout and theoperator <<.

The header file iostream should be included at thebeginning of all programs that use input/outputstatements.

The header files with .h extension are ”old style” fileswhich should be used with old compilers.

Page 10: Beginning with c++

Return Type of main()

In C++ main() returns an integer type value to theoperating system.

Therefore every main() in C++ should end with areturn 0; statement otherwise a warning or an errormight occur.

Since main() returns an integer type value, returntype for main() is explicitly specified as int.

Note that the default return type for all functions inC++ is int.

Page 11: Beginning with c++

Average of Two numbers

#include<iostream>

int main()‏

{

float num1,num2,sum,avg;

cout<<”Enter number 1”;

cin>>num1;

cout<<”Enter number 2”;

cin>>num2;

Page 12: Beginning with c++

Average of Two numbers

sum=num1+num2;

avg=sum/2;

cout<<”Sum =” <<sum <<”\n”;

cout<<”Average =”<<avg <<”\n”;

return 0;

}

Page 13: Beginning with c++

C++ Input Operator

cin>>num1;

Is an input statement and causes the program to waitfor the user to type in a number.

The number keyed in is placed in the variable num1.

The operator >> is known as extraction or get fromoperator.

It extracts the value from the keyboard and assigns it to the variable on its right

Page 14: Beginning with c++

C++ Input Operator

Cout<<” Sum =” <<sum <<”\n”;

First sends the string ”Sum =” to cout and then sendthe value of sum. Finally it sends the newlinecharacter so that the next output will be in the newline.

The multiple use of << in one statement is calledcasading.

Cin>>num1>>num2;

The vlaues are assigned from left to right. 10 & 20entered then num1=10 & num2=20

Page 15: Beginning with c++

Structure of C++ Program

Include Files

Class declaration

Class functions definitions

Main function program

Page 16: Beginning with c++

#include<iostream.h>

#include<conio.h>

class person

{

int roll;

int marks;

public :

void getdata(void)

{

cout<<“Roll No:";

cin>>roll;

cout<<“Marks";

cin>>marks;

}

Page 17: Beginning with c++

void display(void)

{

cout<<"Roll No"<<roll;

cout<<"Marks "<<marks;

}

};

int main()

{

person per1;

per1.getdata();

per1.display();

getch();

return 0;

}