0% found this document useful (0 votes)
5 views

UNIT-III

The document provides an overview of classes in C++, detailing their definition, member functions, access modifiers, and static members. It includes examples of class implementation, member function nesting, and friend functions, along with sample code snippets. Additionally, it presents questions for practice related to class implementation and functionality.

Uploaded by

thakurshah1216
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

UNIT-III

The document provides an overview of classes in C++, detailing their definition, member functions, access modifiers, and static members. It includes examples of class implementation, member function nesting, and friend functions, along with sample code snippets. Additionally, it presents questions for practice related to class implementation and functionality.

Uploaded by

thakurshah1216
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

BLOCK-I

UNIT-III
CLASS

A Class is a user defined data-type which has data members


and member functions.

example of class Car, the data member will be speed


limit, mileage etc and member functions can be apply
brakes, increase speed etc.

When a class is defined, no memory is allocated but when it is


instantiated (i.e. an object is created) memory is allocated.
A class is defined in C++ using keyword class followed by
the name of class. The body of class is defined inside the
curly brackets and terminated by a semicolon at the end.
//C++ program to demonstrate accessing of data members
#include <bits/stdc++.h>
using namespace std;
class Geek
{
// Access specifier
public:

// Data Members
string geekname;

// Member Functions()
void printname()
{
cout << "Geekname is: " << geekname;
}
};

int main() {

// Declare an object of class geeks


Geeks obj1;

// accessing data member


obj1.geekname = "Abhi";

// accessing member function


obj1.printname();
return 0;
}
OUTPUT: Geekname is: Abhi
MEMBER FUNCTIONS IN CLASS
There are 2 ways to define a member function:
1. Inside class definition
2. Outside class definition
Access Modifiers
There are 3 types of access modifiers available in C++:

a) Public

b) Private

c) Protected

• Note: If we do not specify any access modifiers for the members


inside the class then by default the access modifier for the
members will be Private.
Nesting of member functions

A member function can call another member


function of the same class directly without using
the dot operator. This is called as nesting of
member functions.
Passing an Object as argument

To pass an object as an argument we write the


object name as the argument while calling the
function the same way we do it for other
variables.
C++ Friend function
If a function is defined as a friend function in C++, then the protected and private data
of a class can be accessed using the function.

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.

Declaration of friend function in C++

class class_name

friend data_type function_name(argument/s); // syntax of friend function.

};
STATIC MEMBERS
Static data members are class members that are declared using static keywords. A

static member has certain special characteristics. These are:

Only one copy of that member is created for the entire class and is shared by all the

objects of that class, no matter how many objects are created.

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

Static int a //0


#include <iostream.h>
Using namespace std;
Class abc
{
int a,b;
public :
void inputdata()
{
cout<<“Enter Numbers : “;
cin>>a>>b;
}
void dispdata()
{
cout<<“Value of a and b is “ <<a<<b;
}
Void main()
{
abc ob,ob1;
ob.inputdata();
ob.dispdata();
ob1.inputdata();
ob1.dispdata();
}
Syntax of friend function
Friend return_type function_name( class object)
{

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.

You might also like