0% found this document useful (0 votes)
3 views24 pages

Class Part2

The document provides an overview of classes and objects in C++, detailing how to create objects, access class members, and define member functions both inside and outside of class definitions. It also discusses the characteristics of static data members and functions, the concept of nesting member functions, and the use of objects as function arguments. Additionally, it highlights the advantages of the C++ string class over traditional C-strings and includes examples for better understanding.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views24 pages

Class Part2

The document provides an overview of classes and objects in C++, detailing how to create objects, access class members, and define member functions both inside and outside of class definitions. It also discusses the characteristics of static data members and functions, the concept of nesting member functions, and the use of objects as function arguments. Additionally, it highlights the advantages of the C++ string class over traditional C-strings and includes examples for better understanding.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

“CLASS

AND
OBJECT”
CREATING OBJECTS:
• Once a class has been declared we can create variables of that type by using the class name.
Example:
item x;
creates a variables x of type item. In C++, the class variables are known as objects. Therefore x is called an object of type item.
• Item x, y ,z also possible.
• class item
• {
• -----------
• -----------
• -----------
• }x ,y ,z;
• would create the objects x ,y ,z of type item.
• NOTE: Objects are sometimes called instance variables.
ACCESSING CLASS MEMBER

The private data of a class can be accessed only through the member functions of that class. The main() cannot

contains statements that the access number and cost directly.

Syntax:
• object [Link]-name(actual arguments);

Example:- x. getdata(100,75.5);

It assigns value 100 to number, and 75.5 to cost of the object x by implementing the getdata() function .

similarly the statement


• x. putdata ( ); //would display the values of data members.
• x. number = 100 is illegal .

Although x is an object of the type item to which number belongs , the number can be accessed only through a

member function and not by the object directly.


ACCESSING CLASS MEMBER
Example:
class xyz
{
Int x;
Int y;
public:
int z;
};
---------
----------
xyz p;
p. x =0; error . x is private
p, z=10; ok ,z is public
MEMBER DEFINITION FUNCTION
• Member can be defined in two places
1. Outside the class definition
2. Inside the class function
MEMBER DEFINITION FUNCTION CONT.
1. Inside the class Function
• Another method of defining a member function is to replace the function declaration by the actual function definition inside
the class .
Example:
class item
{
Int number; float cost;
public:
void getdata (int a ,float b);
void putdata(void)
{
cout<<number<<endl; cout<<cost<<endl;
}
};
MEMBER DEFINITION FUNCTION CONT.
2. Outside the class Function
• Member function that are declared inside a class have to be defined separately outside the class. Their definition are very
much like the normal functions.
• An important difference between a member function and a normal function is that a member function incorporates a
membership. Identify label in the header. The ‘label’ tells the compiler which class the function belongs to
• Syntax:
return type class-name::function-name(argument declaration )
{
function-body
}
• The member ship label class-name :: tells the compiler that the function function -name belongs to the class class-name.
• That is the scope of the function is restricted to the class-name specified in the header line by scope resolution operator (::).
MEMBER DEFINITION FUNCTION CONT.
Outside the class Function
Example:
void item :: getdata (int a , float b )
{
number=a;
cost=b;
}
void item :: putdata ( void)
{
cout<<”number=:”<<number<<endl;
cout<<”cost=”<<cost<<endl;
}
MEMBER DEFINITION FUNCTION CONT.
The member function have some special characteristics that are
often used in the program development.
• Several different classes can use the same function name. The "membership label" will
resolve their scope,.
• Member functions can access the private data of the class .A non member function
can't do so.
• A member function can call another member function directly, without using the dot
operator
A C++
PROGRAM
WITH CLASS
MAKING OUTSIDE FUNCTION AS A INLINE
• Write Definition outside the class and just place the qualifier “ inline ” in the header line of the function definition.
• Syntax:
class sample
{
private:
int x;
public:
void getdata(int number); //Declaration
};
inline void sample :: getdata(int number) // definition
{
x=number;

}
MORE PROGRAM DEFINITION
• Write a simple program using class in C++ to input subject mark and prints it.
• Write a program to create a class Medicine which stores name of medicine, no of medicine, year of manufacturing
and year of expiry. Class has member function to get and print data.

12
MEMORY ALLOCATION OF OBJECT
• It’s true that each object has its own separate
data items. On the other hand, all the objects in a
given class use the same member functions.
• The member functions are created and placed in
memory only once—when they are defined in the
class definition.
• Data is therefore placed in memory when each
object is defined, so there is a separate set of
data for each object.

13
NESTING OF MEMBER FUNCTION

• 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.

14
PRIVATE MEMBER FUNCTION

• Although it is a normal practice to place all the data


items in a private section and all the functions in
public, some situations may require contain
functions to be hidden from the outside calls. Tasks
such as deleting an account in a customer file or
providing increment to and employee are events of
serious consequences and therefore the functions
handling such tasks should have restricted access.
We can place these functions in the private section.
• A private member function can only be called by
another function that is a member of its class. Even
an object can not invoke a private function using
the dot operator.
ARRAY WITHIN CLASS
• Definition: Member Variable in a class can be declared as a array type is
called Array within class.
• Syntax:
class classname
{
.......
....
datatype arrayname[size];
......
...
};
16
17
STRING CLASS IN C++

• Standard C++ includes a new class called string.


• This class improves on the traditional C-string in many ways.
1. You no longer need to worry about creating an array of
the right size to hold string variables. The string class
assumes all the responsibility for memory management.
2. The string class allows the use of overloaded operators,
so you can concatenate string objects with the + operator:
s3 = s1 + s2
3. This new class is more efficient and safer to use than C-
strings were. In most situations it is the preferred
approach
• The string class uses a number of overloaded operators.
• HOMEWORK: getline() and Different methods available in
String class.

18
Static Data Member
• A data member of a class can be qualified as static . The properties of a static member variable are similar to
that of a static variable. A static member variable has contain special characteristics.
• static datatype variable;
• Variable has contain special characteristics:-
1. It is initialized to zero when the first object of its class is [Link] other initialization is permitted.
2. 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.
3. It is visible only with in the class but its life time is the entire program. Static variables are normally used to
maintain values common to the entire class.
Syntax: datatype classname : : static variable name
For example a static data member of class item can be used as a counter that records the occurrence of all the
objects.
35
int item :: count; // definition of static data member
Static Data Member
• Note that the type and scope of each static member variable must be defined outside the class definition .This is
necessary because the static data members are stored separately rather than as a part of an object.
• As Static data member is associated with class itself rather than with any class object, So , they are also known
as class variables.
• The static Variable count is initialized to Zero when the objects created . The count is incremented whenever
the data is read into an object.
• static variables are like non - inline members as they are declared in a class declaration and defined in the source
file.
• While defining a static variable, some initial value can also be assigned to the variable. for example,
int item : : count =10;

20
Static example

Example: Static Variable is shared among all the


object of the class

Output ?? Output ??
8 8

21
Static Member Function

• A member function that is declared static has following properties :-


a. A static function can have access to only other static members declared in the same class.
b. A static member function can be called using the class name as follows:-
Syntax: Function Definition
static returntype function_name(agument list)
{
// only static members of the class;
}
Function call:
class_name :: function_name();
Object as function argument

• Like any other data type, an object may be used asA function argument. This can done in two ways
1. A copy of the entire object is passed to the function.
2. Only the Reference of the object is transferred to the function
• The first method is called pass-by-value. Since a copy of the object is passed to the function, any change made to
the object inside the function do not affect the object used to call the function.
• The second method is called pass-by-reference . When an reference of the object is passed, the called function
works directly on the actual object used in the call. This means that any changes made to the object inside the
functions will reflect in the actual object .The pass by reference method is more efficient since it requires to
pass only the reference of the object and not the entire object.
Object as function argument
• Example : function definition of add two distance:

• Function call:

24

You might also like