0% found this document useful (0 votes)
14 views62 pages

OOP 2

Uploaded by

vibob98
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)
14 views62 pages

OOP 2

Uploaded by

vibob98
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
You are on page 1/ 62

CHAPTER 2

Classes
Classes

Classes- Class can be defined as a plan of the


object.

It is basically a collection of objects which work as


building blocks.
Generally a class specification has two parts
a) Class declaration
b) Class function definition

the class declaration describes the type and scope of its members.

The class function definition describes how the class functions are implemented.

Syntax:-

class class-name

private:

variable declarations;
function declaration ;
public:
variable declarations;
function declaration;
};
The members that have been declared as private can be accessed only from with in the class.
On the other hand , public members can be accessed from outside the class also.
The data hiding is the key feature of oops.
The use of keywords private is optional by default, the members of a class are private.
The variables declared inside the class are known as data members and the functions are known
as members functions.
Only the member functions can have access to the private data members and private functions.
However, the public members can be accessed from the outside the class.
The binding of data and functions together into a single class type variable is referred to as
encapsulation.
Syntax:-
class item
{
int member;
float cost;
public:
void getdata (int a ,float b);
void putdata (void);
The class item contains two data members and two function members,
the data members are private by default while both the functions are
public by declaration.
The function getdata() can be used to assign values to the member
variables member and cost, and putdata() for displaying their values .
These functions provide the only access to the data members from
outside the class.
Simple Program For Declaring Classes
#include <iostream>
using namespace std;
class smalljob //define a class
{
private:
int somedata; //class data
public:
void setdata(int d) //member function to set data
{
somedata = d;
}
void showdata() //member function to display data
{
cout << "Data is = "<< somedata << endl;
}
};
int main()
{
smalljob s1, s2; //define two objects of class smallobj
s1.setdata(1066); //call member function to set data
s2.setdata(1776);
s1.showdata(); //call member function to display data
s2.showdata();
return 0;
}

The O/P
The Data Is = 1066
The Data Is = 1776
The class smalljob defined in this program contains one data item and two member functions.
The two member functions provide the only access to the data item from outside the class.
The first member function sets the data item to a value, and the second displays the value.
Placing data and functions together into a single entity is a central idea in object-oriented
programming.
This is shown in Figure 6.1
Classes and Objects
An object has the same relationship to a class that a variable has to a data type.
An object is said to be an example of a class,
In smalljob , the class—whose name is smalljob —is defined in
the first part of the program.
Later, in main(), we define two objects—s1 and s2—that are
example of that class.
Each of the two objects is given a value, and each displays its value.
Here’s the output of the program:
Data is 1066 ← object s1 displayed this
Data is 1776 ← object s2 displayed this
We’ll begin by looking in detail at the first part of the program—the
definition of the class smalljob .
Later we’ll focus on what main() does with objects of this class
Defining the Class
Here’s the definition (sometimes called a specifier) for the class smalljob , copied from the
SMALLJOB listing:

class smalljob //define a class

private:

int somedata; //class data

public:

void setdata(int d) //member function to set data


{
somedata = d;
}
void showdata() //member function to display data
{
cout << “\n Data is “ << somedata;
}
};
Defining the Class (cont)
The definition starts with the keyword class, followed by the class name—smalljob
in this

example.

Like a structure, the body of the class is delimited by braces and terminated by a
semicolon.

(Don’t forget the semicolon.

Remember that data structures such as structs and classes end with a semicolon,
while control structures such as functions and loops do not end with a semicolon.)
Private And Public
The body of the class contains two unfamiliar keywords: private and public.

What is their purpose?

A key feature of object-oriented programming is data hiding.

This term does not refer to the activities of particularly paranoid programmers; rather it means
that data is concealed within a class so that it cannot be accessed mistakenly by functions
outside the class.

The primary mechanism for hiding data is to put it in a class and make it private.

Private data or functions can only be accessed from within the class. Public data or functions, on
the other hand, are accessible from outside the class.
Private And Public(cont)
Class Data

The smalljob class contains one data item: somedata, which is of type int.

The data items within a class are called data members (or sometimes member
data).

There can be any number of data members in a class, just as there can be any
number of data items in a structure.

The data member somedata follows the keyword private, so it can be accessed
from within the class, but not from outside.
Member Functions
Member functions are functions that are included within a class.

(In some object-oriented languages, such as Smalltalk, member functions are called
methods; some writers use this term in C++ as well).

There are two member functions in smalljob: setdata() and showdata().

The function bodies of these functions have been written on the same line as the
braces that

delimit them.

You could also use the more traditional format for these function definitions:
void setdata(int d)
{
somedata = d;
}
void showdata()
{
cout << “\n Data is “ << somedata;
}
However, when member functions are small, it is common to compress
their definitions this way to save space.
Because setdata() and showdata() follow the keyword public, they can
be accessed from outside the class.
We’ll see how this is done in a moment.
Figure 6.3 shows the syntax of a class definition.
Functions Are Public, Data Is Private
Usually the data within a class is private and the functions are public. This is a

result of the way classes are used.

The data is hidden so it will be safe from accidental manipulation, while the

functions that operate on the data are public so they can be accessed from outside

the class.

However, there is no rule that says data must be private and functions public; in

sometime you may find you’ll need to use private functions and public data.
Simple Program For Book
#include <iostream>

using namespace std;

class Book

public:

string title;

string author;

int price;

int pages;

};
int main()
{
Book book1;
book1.title = "C++ LEVEL 1";
book1.author = "ALI";
book1.price = 160;
book1.pages = 17;
cout << "Book1" << endl;
cout << "Book1 title " << book1.title <<"\n" << endl;
cout << "Book1 author " << book1.author <<"\n"<< endl;
cout << "Book1 price " << book1.price <<"\n"<< endl;
cout << "Book1 pages " << book1.pages<<"\n" << endl;
return 0;
}
The O/P Is
Book1
Book1 title C++ LEVEL 1

Book1 author ALI

Book1 price 160

Book1 pages 17
Calling Data And Member Function
Member Function

Member functions are functions


that are included within a class
is called member function.
Member function(Cont)
Member function can be defined in two places”

1.Outside the class definition

2.Inside the class definition

The function should perform the same task irrespective of the place of
definition.
Outside definition:
•An important difference between a member function and normal
function is that a member function include a membership ‘identity
label’ in the header i.e class_name::
•This ‘label’ tells the compiler which class the function belongs to.
Syntax
#include <iostream>

using namespace std;


class Cal
{
int x;
int y;
public:
Cal()
{
x = 40;
y = 10;
}
int sum(); // function addition
};
int Cal::sum() // define outside class
{
return x + y;
}
int main()
{
Cal ob;
cout << ob.sum();

}
O/P
50
The member function have special characteristics that are often used in
the program development. Characteristics are:

•Several different classes can use the same function name.

the membership label will resolve their scope.

•Member function can access private data of the class

•The member function can call another member function directly,


without using the dot operator
program with using member function

#include<iostream>

using namespace std;

class Book

public:

string name;
string author;
int price;
// this is call member function and it print all the output

void print()

{
cout << "---------------------"<<"\t"<<endl;
cout << "Name" << "\t" << name << endl;
cout << "Author" << "\t" << author << endl;
cout << " Price" << "\t" << price << endl;
}
};
int main()
{
Book B;

B.name = "c+ level";


B.author = “Ahmed";
B.price = 1000;
B.print();
}
The O/P
Name c+ level
Author Ahmed
Price 1000
Inside class definition:
•Another method of defining member function is to replace
the function declaration by the actual function definition.

•When function is defined inside class is called as inline


function

•Normally small functions are defined inside a class.


Example
#include<iostream>
using namespace std;
class Car
{
public:
string name;
int price;
int model;
// USING MEMEBER FUNCTION(inside the class)
void print()
{
cout << "----------------" << endl;
cout << " Name = " << name << endl;
cout << " Price = " << price << endl;
cout << " Model = " << model << endl;

}
};
int main()
{
Car C,C1;
C.name = "KIAA";
C.price = 1200;
C.model = 2023;
C1.name = "TOYOTA";
C1.price = 10000;
C1.model = 2018;
C.print();
C1.print();
}
Making outside function inline:
when we define member function outside the class definition and still
make it inline by just using the qualifier inline in the header line of
function definition
Write A Program To Create 2 Object And Class
Name as you like (By Using Member Function)
With 3 Variables()
#include<iostream>
using namespace std;
class Car
{
public:
string name;
int price;
int model;
// USING MEMEBER FUNCTION
void print()
{
cout << "----------------" << endl;
cout << " Name = " << name << endl;
cout << " Price = " << price << endl;
cout << " Model = " << model << endl;

}
};

int main()
{
Car C,C1;
C.name = "KIAA";
C.price = 1200;
C.model = 2023;

C1.name = "TOYOTA";
C1.price = 10000;
C1.model = 2018;
C.print();
C1.print();
}
O/P

Name = KIAA
Price = 1200
Model = 2023
----------------
Name = TOYOTA
Price = 10000
Model = 2018
Calling Data
Similar to accessing a data member in the class, we can also
access the public using the dot operator(.).

Example :
int main()
{
class_name obj_name;
Obj_name.member_function_name;
}
Simple program of data calling
#include<iostream>

using namespace std;

class company

public :

int id;

string name;

float salary;
// this is function using to receive values

void save(int i, string n, float s)


{
id = i;
name= n;
salary= s;
}
// this function using to display the output
void display()
{
cout << id <<"\n" << name <<"\n" << salary << "\n" << endl;
}
};
int main()
{
// this object of class

company c;

c.save(1500, "ali", 666.8);

c.display();

return 0;

}
O/P

1500

ali

666.8
Write a program for calling data class name is Book
#include<iostream>
using namespace std;
class Book
{
public:
string name;
int no;
void save(string n, int i)
{
name = n;
no = i;
}
void display()
{
cout << "\n" << name << "\n" << no << endl;

}
};
int main()
{
Book B;
B.save("JAVA", 20);
B.display();
return 0;
}
O/P
JAVA
20
Scope Resolution Operators
The Scope Resolution Operators is used to reference the global
variable or member function that is out of scope.

Therefore, we use the Scope Resolution Operators to access the


hidden variable or function of program.

The operator is represented as the double colon(::)symbol.


Purposes Of Scope Resolution Operators
 To access a global variable when there is a local variable with same name.

 To define a function outside a class.

 To access a class’s static variables

 In case of multiple inheritance

 For namespace

 Refer to a class inside another class


Pointer to object
The pointer is a special built-in pointer that is available to a class’s
member functions.

It always points to the instance of the class making the function call.

Syntax for pointer object

Class_name object_ name;


#include<iostream>
using namespace std;
class Rect
{
public:

int width;
int lenght;
int height;
int print();
};
int Rect::print()
{
return width*lenght*height;
}
int main()
{
Rect op;

op.width = 2;
op.lenght = 3;
op.height = 5;
cout<<op.print()<<endl;
return 0;

}
a)Write a class program using member function and
the name of class is patient with 4 object and no
less than with 3 variables?

b)Write a class program using data member name of


class is employee with 3 object and no less than with
3 variables?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy