Beruflich Dokumente
Kultur Dokumente
Part-2
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.
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.
class class_name
{
private:
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
class item
{
int number;
floar cost;
public:
void getdata(int a, float b);
void putdata(void);
}; //ends with semicolon
Creating Objects:
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()