Sie sind auf Seite 1von 6

Schools Interaction Initiative

Tutorial on Beginning with C++

Part-2

Rajalakshmi Institute of Technology, Chennai


Functions:

Now’s the time to introduce the fun stuff in programming, Functions.

What is a Function?
A function is a group of statements that together perform a task. Every C++ program has
at least one function, which is main(), and all the most trivial programs can define
additional functions.

Let’s use Functions ins a program.


#include<iostream.h>

int a, b, c; //declaring Global variables

void add() //declaring a function


{
c = a + b;
cout << “Sum is: ” << c;
}
void main()
{
cout<< “Enter the values of a and b: ”;
cin >> a >> b;
add();
cout << ”End of Program” //calling the function
}
Output:
Enter the values of a and b: 3 5

Rajalakshmi Institute of Technology, Chennai


Sum is: 8
End of Program

Explanation of the program:


• As usually the program starts with the main() function.
• First it displays the cout<<
• Then it gets the input from the user as cin>> is used.
• The next line says add(), this line redirects the program to the previous lines where
we have declared the void add() function
• Then the program inside the function is executed.
• If the program inside the function is completed then the control flow is redirected
to the main() function itself.

The above program is just a basic version on how to use a function simply.
You can read further about function in this link: bit.ly/2ZErWML

Now we’re going to get into the core of Object oriented programming,Classes.

Classes:
• A class is a way to bind the data and its associated functions together.
• It allows the data (and functions) to be hidden, if necessary, from external use.
• When defining a class, we are creating a new abstract data type that can be treated
like any other built-in data type.
• Generally, a class specification has two parts:
• Class declaration
• Class function definitions

Specifying a Class:
• The class declaration describes the type and scope of its members.
• The class function definitions describe how the class functions are implemented.

Rajalakshmi Institute of Technology, Chennai


• The general form of a class declaration is:

class class_name
{
private:
variable declaration;
function declaration;
public:
variable declaration;
function declaration;

Simple Example for classes:

class item
{
int number;
floar cost;
public:
void getdata(int a, float b);
void putdata(void);
}; //ends with semicolon

Creating Objects:

We may also declare more than one object in one statement.


Example: item x, y, z;

Rajalakshmi Institute of Technology, Chennai


A one full example for all the concepts in the previous pages are coming up.

Program to find print the even and odd numbers according to the user preference.

#include<iostream.h>
class Demo
{
int choice, upLimit;
public:
void getInputs()
{
cout<< ”Enter Up limit for the numbers:”;
cin>> upLimit;
cout<< “What do you want to print? \n” << ”1.Even numbers or 2.Odd numbers: ”;
cin>> choice;
}
void display()
{
if(choice == 1)
for(int i = 0; i < upLimit; i++)
if(i%2 == 0)
cout<< i << “\n” ;
if(choice == 2)
for(int i = 0; i < upLimit; i++)
if(i%2 != 0)
cout<< i << “\n” ;
}
};
void main()

Rajalakshmi Institute of Technology, Chennai


{
Demo object1;
object1.getInputs();
object1.display();
}

Rajalakshmi Institute of Technology, Chennai

Das könnte Ihnen auch gefallen