C Class Methods

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

C++ class Methods

What is class
A class is an organization of data and functions which operate on them.
The data and functions within a class are called members of the class.
Data : int a, float a etc
Functions : hello(). Lpu()
The combination of data members and member functions constitute a
data object or simply an object.
 General Structure of a class
Class name or name of class
Data Members
Member functions
Access Specifiers
Declaring objects
 A class is used to specify the form of an object and it combines data
representation and methods for ma. The data and functions within a
class are called members of the class.
 Methods are functions that belongs to the class.
 There are two ways to define functions that belongs to a class:
• Inside class definition
• Outside class definition
 You access methods just like you access attributes; by creating an
object of the class and using the dot syntax (.)
#include <iostream>
using namespace std;
class MyClass { // The class
public: // Access specifier
void myMethod() { // Method/function
cout << "Hello World!";
}
};
int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}
define a function outside the class definition,
 have to declare it inside the class and then define it outside of the
class.
 This is done by specifiying the name of the class, followed the scope
resolution :: operator, followed by the name of the function
#include <iostream>
using namespace std;

class MyClass { // The class


public: // Access specifier
void myMethod(); // Method/function declaration
};

// Method/function definition outside the class


void MyClass::myMethod() {
cout << "Hello World!";
}

int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}
Parameters
You can also add parameters
#include <iostream>
using namespace std;
class Car {
public:
int speed(int maxSpeed);
};
int Car::speed(int maxSpeed) {
return maxSpeed;
}
int main() {
Car myObj;
cout << myObj.speed(200);
return 0;
}
Nesting of Member Functions
 A member function of a class can be called only by an object
of that class using a dot operator. However, there is an
exception to this
 A member function can be called by using its name inside
another member function of the same class. This is known as
nesting of member functions.
Nesting of member function
void disp_num()
#include<iostream>
{
using namespace std;
class nest int sq=square_num();
{ int a; int cu=cube_num();
int square_num( ) cout<<"The square is "<<sq;
{
cout<<"The cube is "<<cu;
return a* a;
} }
public: };
void input_num( ) int main()
{
{ nest n1;
cout<<"Enter a number";
n1.input_num();
cin>>a;
} n1.disp_num();
int cube_num( ) }
{
return a*a*a;
}
Array within class void show()
#include<iostream> {
using namespace std; cout<<"Array elements are"<<endl;
class array for(int i=0;i<6;i++)
{ {
int a[6]; cout<<a[i]<<endl;
public:
}
void getdata()
}
{
};
cout<<"Enter 6 numbers in the
main()
array"<<endl;
{ array obj;
for(int i=0;i<6;i++)
obj.getdata();
{
obj.show();
cin>>a[i];
}
}
}
• Private member functions
• A function declared inside the private access specifier of the
class, is known as a private member function.
• A private member function can be accessed through the only
public member function of the same class.
#include <iostream> int main ( )
using namespace std;
class student {
{ private:
int rollno; student ram;
float fees;
void read() ram.show ( );
{ rollno=12;
fees=145.10; }
}
public:
void show()
{
read();
cout<<” \n Rollno =
"<<rollno;
cout<<"\n Fees = "<<fees;
}
};

You might also like