LITTLE ABOUT LESOTHO FROM THE TIME MOSHOESHOE THE FIRST WAS BORN
Classes in c++ (OOP Presentation)
1.
2. Object-oriented programming
o Encapsulates(group related data and functions together)
data (attributes) and functions (behavior) into packages
called
o Classes are the standard unit of OOP
oObjects are instantiated (created) from the class
For example, a Honda Civic is an instance of a Class Car
3. Class is a user defined data type.
Name begins with a keyword CLASS .
Body is written in { };
Class class name
{
……. …….. …….
… …. … … … …
……. ……….
};
Keyword
Any name
of classs
Body
Data Members.
Member functions.
4. Members of a class can be restricted or controlled on its
access within and outside that class using access specifier.
o There are 3 access specifiers in C++:
•Public.
• Private.
• protected.
Usually, the data members of a class are declared in the
private section of the class.
The member functions are in public section.
5. #include<iostream>
using namespace std;
class value
{
private:
int variable;
public:
void input()
{
cout << "Enter an integer:";
cin >> variable;
}
void output()
{
cout << "Variable entered is:”<< variable;
}
};
void main()
{ value obj;
obj.input();
obj.output();
getchar();
getchar();
}
Enter an integer: 6
Variable entered is: 6
6. Member functions can be defined outside the class
using resolution operator (::).
Format for defining member functions outside the
class:
ReturnType ClassName::MemberFunctionName()
{
….
}
7. Special Member Functions
Constructor:
o Same name as class
o No return type
Destructors:
o Same name as class
• Preceded with (~)
8. # include<iostream>
using namespace std;
class add
{
private:
int x; int y;
public:
void input( )
{
cout<<“enter two integer:”;
cin>>x>>y;
}
Void output( )
{
cout<<“sum of entered integer is:”<<x+y<<endl;
}
};
class difference
{
private:
int a; int b;
public:
void get( )