UNIT-III
UNIT-III
UNIT-III
CLASS
// Data Members
string geekname;
// Member Functions()
void printname()
{
cout << "Geekname is: " << geekname;
}
};
int main() {
a) Public
b) Private
c) Protected
By using the keyword friend compiler knows the given function is a friend function.
For accessing the data, the declaration of a friend function should be done inside the
body of a class starting with the keyword friend.
class class_name
};
STATIC MEMBERS
Static data members are class members that are declared using static keywords. A
Only one copy of that member is created for the entire class and is shared by all the
It is initialized to zero when the first object of its class is created. No other initialization
is permitted
It is visible only within the class, but its lifetime is the entire program
Syntax
static data_type data_member_name;
E.G.
Int a ;(garbage)
Static int a;(0)
Class employee
{
int a;
Public:
void getdata(); : ::
void dispdata(); scope resolution operator
};
Void employee::getdata()
{
cout<<“Enter Number :”;
cin>>a;
};
Void employee::dispdata()
{
cout<<“Entered number is : “<<a;
};
Main()
{
employee e;
e.getdata();
e.dispdata();
}
Int a //garbage value
Body
}
#include<iostream.h>
Class A
{
Private :
int a,b;
public:
void input()
{
cout<<“Enter Number : “;
cin>>a>>b;
}
friend void add(A ob);
};
Void add(A ob)
{
int c;
c=ob.a + ob.b;
cout<<“Sum : “<<c;
}
Void main()
{
a kk;
kk.input();
add(kk);
}
Class add Void main()
{ {
int a,b,c; add ob;
public: ob.get();
void get(); ob.disp();
void disp(); }
};
Void add : : get() :: scope resolution
{ operator
cout<<“Enter Any Two Numbers : “;
cin>>a>>b;
c=a+b;
}
Void add :: disp()
{
cout<<c;
}
Questions
1.WAP to implement class.(swap values of two
numbers)
2.WAP to find simple interest using class.
3. WAP to implement friend function.(square of given
number)
4. WAP to display table of given number by using class.
5. WAP to make a simple calculator to add, subtract,
multiplication and division by using scope resolution
operator.