Global Variables
Global Variable
Global Variable
int m=20; //local to main
int k=m;
int m=30;
cout<<k;
cout<<m;
cout<<::m;
cout<<m;
cout<<::m;
Functions
Defining a Function�
Example
Function Prototyping
Type function_name(argu_list);
E.g. float volume(int x,float y,float z);
E.g. float volume(int x,float y,z); // illegal
So, float volume(int,float,float);
Float volume(int a,float b,float c)
{
Float v=a*b*c;
}
Prototyping Example
#include <iostream.h>
Float square(float);
Void main()
{
Float x,sqx;
Cout<<“enter no.”;
Cin>>x;
Sqx=square(x);
Cout<<sqx;
}
Float square(float a)
{
Float s;
S=a*a;
return(s);
}
Default Arguments
float amount(float principal,int time,float rate=0.15);
Value=amount(5000,7); // one missing argu
i.e.Default value of 0.15 for rate of interest.
Value=amount(5000,5,0.12); passes an explicit value of 0.12 to rate.
int mul(int i,int j=5,int k=10);
int mul(int i=5,int j);
int mul(int i=0,int j,int k=10);
int mul(int i=2,int j=5,int k=10);
Function Overloading
Add() function
//Function declaration
//Function Call
Default arguments & Function overloading Example
Void sum(int x=10,int y=20);
Void main()
{
Sum();
Sum(5);
Sum(5,15);
}
Void sum(int x,int y)
{
Int z=x+y;
Cout<<z;
}
C++ Class Definitions�
Define C++ Objects�
Accessing the Data Members�
Class A
{
int somedata;
public:
void setdata(int d)
{
somedata=d;
}
void showdata()
{
cout<<somedata;
}
};
void main()
{
A s1,s2;
s1.setdata(10);
s2.setdata(20);
s1.showdata();
s2.showdata();
}
Static Data Members
Characteristics:-
1. It is initialized to zero when the first object of class is created.
-No other initialization is permitted.
2.Only one copy of that member is created for the entire class and shared by all the objects of that class.
-No matter how many objects are created.
int item::count;�must be defined outside the class.�Initial value can also be assigned to the variable.�
Class item
{
Static int count;
Public:
getdata()
{
count ++;
}
getcount()
{
cout<<count;
}
};
Int main()
{
item a,b,c;
a.getcount();
b. getcount();
c.getcount();
a.getdata();
b.getdata();
c.getdata();
a.getcount()
b.getcount();
c.getcount();
}
Class A
{
Public:
Static int value;
};
int a::value=1;
int main()
{
A s1;
s1.value=2;
A s2;
cout<<s2.value;
return 0;
}
Static Member Functions
Properties:-
2. A static member function can be called using the class name(instead of its objects).
#include <iostream>
using namespace std;
class test
{
static int count;
int code;
public:
void setcode()
{ code=++count;
}
void showcode()
{
cout<<code;
}
static void showcount(void)
{
cout<<count;
}
};
int test::count;
int main()
{
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}
CONSTRUCTORS�
Constructors�
Example�
class MyClass
{ // The class� public: // Access specifier� MyClass()
{ // Constructor� cout << "Hello World!";� }�};��int main()
{� MyClass myObj;
// Create an object of MyClass (this will call the constructor)�
return 0;
}
Special characteristics of Constructors
Types of Constructors
C++ offers four types of constructors.
These are:
Do nothing constructors
Default Constructor
Parameterized Constructor
�
Copy Constructor
Constructor Overloading/�Multiple constructors in a Class�
{m=a;n=b;}
{m=i.m;n=i.n}
1. receives no argument
2.receive two integer arguments.
3. receive one integer object as an argument.
or
integer i3=i2;
Destructors
Destructor
~ integer()
{
}
�C++ Destructor Example��
class ABC
{
public:
ABC ()
{
cout << "Hey look I am in constructor" << endl;
}
~ABC()
{
cout << "Hey look I am in destructor" << endl;
}
};
int main()
{
ABC cc1;
cout << "function main is terminating...." << endl;
return 0;
}
Output
int count=0;
class alpha
{
public:
alpha()
{
count++;
cout<<count;
}
~alpha()
{
cout<<count;
count--;
}
};
int main()
{
alpha a1,a2,a3,a4;
{
alpha a5;
}
{
alpha a6;
}
return 0;
}
Output
class ABC {
private:
int x,y;
public:
ABC ()
{
x = y = 0;
}
ABC(int a)
{
x = y = a;
}
ABC(int a,int b)
{
x = a; y = b;
}
void display()
{
cout << "x = " << x << " and " << "y = " << y << endl;
}
};
int main()
{
ABC cc1;
ABC cc2(10);
ABC cc3(10,20);
cc1.display();
cc2.display();
cc3.display();
return 0;
}
Output