Advance C++


Classes tutorials
-
A class is a user defined data type.
-
It is the fundamental packaging unit of OOP technology. The programming construct of a class binds the data and functions together to produce group of similar objects.
-
It allows the data and function to be hidden, if necessary from external use.
-
In C terms class is the natural evolution of a structure.
-
Classes contain not only data but also functions. The functions are called as the member functions of the classes.
-
The class allows the data and function to be hidden, if necessary, from external use.
-
It is called as a template of an object. Class is also called as definition for an abstract data type.
-
The class specifies the type and scope of its members.
Class syntax
class class_name
{
//body of a class
};
-
The keyword class indicates that the name which follows is an abstract data type.
-
The body of a class is enclosed within the curly braces followed by a semicolon at the end of the class specification.
-
The body of a class contains declaration of variables and functions, collectively known as members.
-
The variables declared inside a class are known as data members and functions are known as member functions.
-
These members are usually grouped under two sections private and public which define the visibility of data members.
Class members
-
A class has two kinds of members
Data members
Member functions
Data members
-
All the primary (int,char,float,double,bool) and the secondary data types (arrays,pointers,structures,union,variables and class objects) can be the data member of a class.
-
Data members can be classified into two groups
Regular
Static
Regular
-
When an object is created every object gets a copy of all the regular members of the class.
Static
-
Only one copy of static variable is created. All the objects share the same copy of the variable.
Member functions
-
A function that is defined inside a class declaration is called as member function. In general all the member functions are defined in the public area of the class. It can also be defined in the private area then only the member function of the class can access the private member functions. Member functions are also called as methods.
-
Member functions can be defined in two ways. It can be defined inside the class or it can be declared inside the class and defined outside the class using scope resolution (::) operator.
Different types of functions in a class
Regular functions
Overloaded functions
Inline functions
Friend functions
Static functions
Const functions
Constructors
Destructors
Virtual functions
Program to find the largest of three numbers by using class and its member functions |
|
|
//WAP to find the largest of three numbers by using class and its member functions#include <iostream.h>#include <conio.h>class Number{private:float num;public: void read(){cout << "Enter a value: ";cin >> num;}void display(){cout <<"The number is: "<< num << endl;}void largedisplay(Number a, Number b){int max = (a.num > b.num)? a.num : b.num;cout << "Max Value = " << (num > max ? num : max) << endl;}};void main(){clrscr();Number x, y, z;x.read(); y.read(); z.read();x.largedisplay(y, z);getch();} |
Output |
Enter a value: 4Enter a value: 3Enter a value: 2Max Value = 4 |
Program to print fibonacci series using classes |
|
|
//WAP to print fibonacci series using classes#include<iostream.h>#include <conio.h>class fibo{int i,j,k;public:fibo(){i=1;k=1;}void fibonacci (int n){int p;for (p=0;p<n;p++){j=i+k;i=k;cout<<k<<" ";k=j;}}};void main(){fibo a;int n;cout <<"Enter the terms of fibonacci series: ";cin>>n;a.fibonacci(n);getch();} |
Output |
Enter the terms of fibonacci series: 61 2 3 5 8 13 |