Classes in C++

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 6

Classes In C++

Classes

It’s a blueprint for a data type. This doesn't actually define any data, but it does define what the class name means,
that is, what an object of the class will consist of and what operations can be performed on such an object.

A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of
curly braces.

The keyword public determines the access attributes of the members of the class that follows it.

A public member can be accessed from outside the class anywhere within the scope of the class object. You can also
specify the members of a class as private or protected .
Friend Function
● Friend functions of the class are granted access to private and protected members of the class in C++.

● Friend functions are not member functions of the class.

● The friend function is declared using the friend keyword inside the body of the class.

● We use them when we need to operate between two different classes at the same time.

● The function is not in the ‘scope’ of the class to which it has been declared a friend.

● It cannot be invoked using the object as it is not in the scope of that class.
Friend Function
● We can invoke it like any normal function of the class.

● Friend functions have objects as arguments.

● It cannot access the member names directly and has to use dot membership operator and use an object name
with the member name.

● We can declare it either in the ‘public’ or the ‘private’ part.


Practice Questions
1.WAP to create Simple class and object.

2.WAP to create a Student class and which has student name, standard and percentage as data members.
Access them using class.

3.WAP to create a Student class and which has student name, standard and percentage as data members.
Access them using functions

4.WAP to add two numbers and three numbers using concept of function overloading.

5.WAP to create a class named “Distance” read and add two distances in inches and feet using class and
object and return type should be class type..

6.WAP to read a number and print a number using friend function.


Assignment

1. WAP to add two numbers using class. Access the data members using member
functions. Initialize the data members using constructor.
2. WAP to Convert time from seconds to HH:MM:SS format using class program
in C++ and return type should be class type.
3. WAP with function named volume to find volume of a cube, cuboid and sphere.
Write functions with same name. Use the concept of function overloading.
4. WAP to implement Linear Search algorithm using classes in C++.
5. WAP to implement an algorithm of Binary Search using classes in C++.

You might also like