Search for notes by fellow students, in your own course and all over the country.
Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
Classes and Objects
Introduction
• Just an extension of idea of structures in C
• A new way of creating and implementing userdefined data type
2
Extensions to Structures
•
•
•
•
•
In C++, structures can have both variables and functions
struct student
{
char name[20];
int rollno;
char grade[2];
};
struct student krishna;
//Allowed in C
student krishna;
//Error in C but allowed in C++
In C++, structure names are stand-alone and can be used like any other type
names
All these extensions are incorporated in a user defined type called class in C++
Class is a specially designed data type in C++
Structure are normally used for holding the data and classes for holding both data
and function
3
Creating classes
• A class is a way to bind data and its associated
functions together
• Class allows data and functions hiding
• When defining a class, we are creating a new
abstract data type
• A class specifications has:
– Class declaration
– Class function definitions
4
Creating classes
keyword
class class_name
{
private:
visibility labels
data members
variable declarations;
function declarations;
class members
variable declarations;
function declarations;
class members
public:
};
Member functions
• The binding of data and functions together into a single classtype variable is referred to as encapsulation
5
Example
class student
{
int roll_number;
float marks;
public:
Class: STUDENT
DATA
roll_number
marks
……
...
void insertdata(int a, float b);
void displaydata(void);
STUDENT
};
inserttdata()
displaydata()
………
...
Therefore, a is
an object of type student
student a, b, c ; //More than one object declaration
class student
{
………
……
...
}a,b,c;
7
Accessing Class Members
• The private data of a class can be accessed only
through the member functions of that class
• The main() function can not contain statements that
access roll and marks directly
Object-name
...
insertdata(10, 85
...
displaydata();
insertdata(10, 85
...
Member functions can be invoked only by using object
a
...
displaydata(); sends a message to object a
requesting it to display its contents
• A variable declared as public can be accessed by the
objects directly class xyz
{
int x;
int y;
public:
int z;
};
……
...
xyz p;
p
...
z=100;
//Error
//Correct, z is public
9
Defining Member Functions
• Two ways:
– Outside the class definition
– Inside the class definition
10
Defining Member Functions
• Outside the class definition
– Member functions defined inside a class have to be defined separately
outside the class
– The difference between a member function and a normal function is that
a member function incorporates a membership ‘identity label’ in the
header
– This ‘label’ tells the compiler which class the function belongs to
return-type class-name::function-name (argument declaration)
{
Function body
}
– The membership label class-name:: tells the compiler that the functionname belongs to the class-name
11
Example
void student::insertdata(int a, float b)
{
roll_number = a;
marks = b
}
void student::displaydata(void)
{
cout<<“Roll Number: ”<
•
Characteristics of Member functions
– Different classes can use same function name
...
A non-member function can’t do so
(Exception: friend function)
– A member function can call another member function directly, without using the dot operator
...
)
void student::insertdata(int a, float b)
{
roll_number = a;
marks = b;
}
15
An Example (Contd
...
insertdata(student_roll, student_marks); //Call member functions of class student
x
...
)
void student::insertdata(char a[], int b, float c)
{
for(int i=0;i<20;i++)
{
name[i] = a[i];
}
roll_number = b;
marks = c;
}
18
An Example2 (Contd
...
insertdata(student_name, student_roll, student_marks); //Call member
x
...
change(val1,val2);
cin>>val1>>val2;
s2
...
26
static Class Members
• Use static data members to save storage when a single
copy of the data for all objects of a class will suffice
...
• A static function cannot include non- static function but it can
incorporate other static function
• You do not need to create object in order to call static
function from outside the class example is given
– A static member function is one which can be called directly form
main ( ) without associating it to an object
– static member function can be called without creating instances /
objects of the class
...
change(val1,val2);
cin>>val1>>val2;
s2
...
change(val1,val2);
cin>>val1>>val2;
s2
...
getcount();
s2
...
this can be done by
declaring a non member function friend to a class whose
private data is to be accessed
...
So friend function cannot be called
with the help of object of that class
...
• 3→ Friend function cannot access member name directly
...
• 4→ Friend function can be declared either in public or private
part of the class with out affecting its meaning
...
• 6→ usually , friend function has object as argument
...
a+s
...
0; // accessing private
data with help of object of that class
}
main()
{
sample x;
x
...
a+s
...
0; // accessing private
data with help of object of that class
}
main()
{
sample x;
x
...
a+s2
...
0;
}
main()
{
sample1 x;
x
...
setvalue();
cout<
return 0;
}
34
Friend Class
• We know that member of one class can not access the private
data of other class
...
In such
case we need to make one class to a friend of other class
...
object of
class A can access private data of class B if class A is a friend to
Class B
...
• It is a member function of one class and uses object of other
class as arguments so the function has to be called using
corresponding class object and other class object as
arguments
...
b<
int main()
{
test1 x;
x
...
setvalue();
x