A.) Intro to classes
In C++, the class forms the basis for object-oriented programming. The class is used to define the nature of an object, and It is C++'s basic unit of encapsulation. It is a group of entities that share a common definition and that therefore share common properties, operations, and behavior.
Creating a Class:
Classes are created using the keyword class. A class declaration defines a new type that links code and data. This new type is then used to declare objects of that class. Thus, a class is a logical abstraction, but an object has physical existence. In other words, an object is an instance of a class.
General Class Declaration:
A class declaration is similar syntactically to a structure. The general form of a class declaration that does not inherit any other class is shown below:
Example:
class class-name
{
private data and functions
access-specifier:
data and functions
access-specifier:
data and functions
// ...
access-specifier:
data and functions
} object-list;
The object-list is optional.If present, it declares object of the class.
B. ) Access Specifier
- Access specifiers are used to set boundaries for availability of members of class be it data members or member functions.
- Access Specifier is one of these three C++ keywords:
- Private: By default, functions and data declared within a class are private to the class and may be accessed only by other members of the class.
- Public: The public access specifier allows functions or data to be accessible to other parts of your program.
- Protected: The protected access specifier is needed only when inheritance is involved. Once an access specifiers has been used, it remains in effect until either another access specifier is encountered or the end of the class declaration is reached.
C. ) Intro to objects
What are Objects?
A class represents a set of objects that share a common structure and a common behavior, whereas an object is an instance of a class.
Syntax to declare object
class className objectName1,objectName2...;
or
className objectName,objectName2..;
Example:
class Employee emp1;
// or
Employee emp1;
Declaration of an object is similar to any basic type. Here keyword class is optional.Employee class creates object emp1. More than one object can also be created with single statement as follows. Employee emp1,emp2,emp3; Objects can also be created by placing their names immediately after the closing brace.
Example:
class Employee
{
.
.
.
} emp1,emp2,emp3;
would create objects emp1,emp2, and emp3 of the Employee.
Declaring object using new operator
Employee *emp;
emp = new Employee;
The first line of code defines a pointer designed to point to an Employee object. The second line uses new to create an Employee object. new returns a pointer to the newly created Employee.
D.) Access class members
Member functions and Member Variables
Functions that are declared within a class are called member functions. Member functions may access any element of the class of which they are a part. This includes all private elements. Variables that are elements of a class are called member variables or data members.
There are a few restrictions that apply to class members:
- A non-static member variable cannot have an initializer.
- No member can be an object of the class that is being declared. (Although a member can be a pointer to the class that is being declared.)
- No member can be declared as auto, extern, or register.
- In general, you should make all data members of a class private to that class. This. is part of the way that encapsulation is achieved. However, there may be Situations In which you will need to make one or more variables public. For example, a heavily used variable may need to be accessible globally in order to achieve faster run times.
Example of accessing public class members
#include<iostream>
using namespace std;
class myclass
{
public:
//accessible to entire program
int i,j,k;
};
int main ()
{
myclass x,y;
//access to i,j, and k is OK
x.i = 20;
x.j = 5;
x.k = x.i * x.j; //remember x.k and y.k are different
y.k = 15;
cout<<x.k<<" "<<y.k;
return 0;
}
Output :
100 15
E.) Inheritance
i.) Introduction to Inheritance
Inheritance is a process of creating new classes from existing classes. The derived class inherits some or all of the traits from the base class.
Description :
- Main purpose of inheritance is make code reusable.
- The old class is referred to as the base class and the new classes, which are inherited from the base class, are called derived classes.
- When we inherit a class, all its variable fields and methods become available in derived class.
- Child class can access all member variable and function except constructor, copy constructor, destructor.
- Child or derived class can only access public and protected members, cannot use private field.
- In C++, we have 5 different types of Inheritance. Namely :-
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance (Virtual Inheritance)
Syntax :
class SubclassName : access_specifier SuperclassName
//NOTE: here access specifier can be public,private,protected.
ii.) Single Inheritance
In this type of inheritance one derived class inherits from only one base class. It is the simplest form of Inheritance.
#include<iostream>
#include<conio.h>
using namespace std;
class BASECLASS
{
int a;
public:
int b;
void get_ab();
int get_a();
void show_a();
};
class DERIVEDCLASS: private BASECLASS
{
int c;
public:
void mul();
void display();
};
void BASECLASS::get_ab()
{
cout<<"Enter Values for a and b for multiplication : ";
cin>>a>>b;
}
int BASECLASS::get_a()
{
return a;
}
void BASECLASS::show_a()
{
cout<<"a= "<<a<<"\n";
}
void DERIVEDCLASS::mul()
{
get_ab();
c=b*get_a();
}
void DERIVEDCLASS:: display()
{
show_a();
cout<<"b= "<<b<<"\n";
cout<<"c= "<<c<<"\n\n";
}
int main()
{
DERIVEDCLASS d;
d.mul();
d.display();
d.mul();
d.display();
}
Output :
Enter Values for a and b for multiplication :
2
4
a= 2
b= 4
c= 8
Enter Values for a and befor multiplication :
6
7
a= 6
b= 7
c= 42
iii.) Multiple Inheritance
In this type of inheritance a single derived class may inherit from two or more than two base classes.
#include <iostream>
using namespace std;
class Area
{
public:
float area_calc(float l,float b)
{
return l*b;
}
};
class Perimeter
{
public:
float peri_calc(float l,float b)
{
return 2*(l+b);
}
};
//Rectangle class is derived from classes Area and Perimeter.
class Rectangle : private Area, private Perimeter
{
private:
float length, breadth;
public:
Rectangle() : length(0.0), breadth(0.0) { }
void get_data( )
{
cout<<"Enter length: ";
cin>>length;
cout<<"Enter breadth: ";
cin>>breadth;
}
float area_calc()
{
//Calls area_calc() of class Area and returns it.
return Area::area_calc(length,breadth);
}
float peri_calc()
{
//Calls peri_calc() function of class Perimeter and returns it.
return Perimeter::peri_calc(length,breadth);
}
};
int main()
{
Rectangle r;
r.get_data();
cout<<"Area = "<<r.area_calc();
cout<<"\nPerimeter = "<<r.peri_calc();
return 0;
}
OUTPUT :
Enter length: 3
Enter breadth: 5
Area = 15
Perimeter = 16
iv.) Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherits from a single base class.
#include<iostream>
using namespace std;
class Shape
{
protected:
float width, height;
public:
void set_data (float a, float b)
{
width = a;
height = b;
}
};
//inheriting Shape class
class Rectangle: public Shape
{
public:
float area ()
{
return (width * height);
}
};
//inheriting Shape class
class Triangle: public Shape
{
public:
float area ()
{
return (width * height / 2);
}
};
int main ()
{
Rectangle rect;
Triangle tri;
rect.set_data (5,3);
tri.set_data (2,5);
cout <<"Area of Rectangle : " <<rect.area() << endl;
cout <<"Area of Triangle : "<< tri.area() << endl;
return 0;
}
Output :
Area of Rectangle : 45
Area of Triangle : 55
v.) Multilevel Inheritance
In this type of inheritance, the derived class inherits from a class, which in turn inherits from some other class. The Superclass for one is sub-class for the other.
#include<string.h>
using namespace std;
class student
{
private:
int rl;
char nm[20];
public:
void read();
void display();
};
class marks : public student
{
protected:
int s1;
int s2;
int s3;
public:
void getmarks();
void putmarks();
};
class result : public marks
{
private:
int t;
float p;
char div[10];
public:
void process();
void printresult();
};
void student::read()
{
cout<<"Enter Roll no. : ";
cin>>rl;
cout<<"Enter Name : ";
cin>>nm;
}
void student:: display()
{
cout <<"\nRoll no. : "<<rl<<endl;
cout<<"Name : "<<nm<<endl;
}
void marks ::getmarks()
{
cout<<"Enter marks for 3 subjects : "<<endl;
cin>>s1>>s2>>s3;
}
void marks ::putmarks()
{
cout<<"Subject 1 :"<<s1<<endl;
cout<<"Subject 2 : "<<s2<<endl;
cout<<"Subject 3 : "<<s3<<endl;
}
void result::process()
{
t= s1+s2+s3;
p = t/3.0;
p>=60?strcpy(div,"First"):p>=50?strcpy(div, "Second"): strcpy(div,"Third");
}
void result::printresult()
{
cout<<"Total = "<<t<<endl;
cout<<"Percentage = "<<p<<endl;
cout<<"Division = "<<div<<endl;
}
int main()
{
result x;
x.read();
x.getmarks();
x.process();
x.display();
x.putmarks();
x.printresult();
8.2
return 0;
}
Output :
Enter Roll no. : 1
Enter Name : ABC
Enter marks for 3 subjects :
60
90
68
Roll no. : 1
Name : ABC
Subject 1 :60
Subject 2 : 90
Subject 3 : 68
Total = 218
Percentage = 72.6667
Division = First
vi.) Hybrid Inheritance
Hybrid Inheritance is combination of Hierarchical and Multilevel Inheritance.
#include<conio.h>
using namespace std;
class A
{
public:
int l;
void len()
{
cout<<"\nLength : ";
cin>>l;
}
};
class B :public A
{
public:
int b,c;
void l_into_b()
{
len();
cout<<"\nBreadth : ";
cin>>b;
c=b*l;
}
};
class C
{
public:
int h;
void height()
{
cout<<"\nHeight : ";
cin>>h;
}
};
//Hybrid Inheritance Level
class D:public B,public C
{
public:
int res;
void result()
{
l_into_b();
height();
res=h*c;
cout<<"\nResult (l*b*h) : "<<res;
}
};
int main()
{
D d1;
d1.result();
getch();
}
Output :
Length : 8
Breadth : 3
Height : 4
Result (l*b*h) : 96
retu
