Industrial Training File
Industrial Training File
Syntax:
class <class_name>{
//code;
};
Syntax:
class_name object_name;
Example:
#include<iostream>
#include<conio.h>
class student{
public:
char name[20],branch[20],rollno[20];
};
int main(){
system("cls");
cin>>s.name;
cin>>s.rollno;
cin>>s.branch;
getch();
return 0;
Encapsulation
In normal terms Encapsulation is defined as wrapping up of data and
information under a single unit. In Object Oriented Programming,
Encapsulation is defined as binding together the data and the functions
that manipulates them.
Encapsulation also lead to data abstraction or hiding. As using
encapsulation also hides the data. In the above example the data of
any of the section like sales, finance or accounts is hidden from any
other section.
In C++ encapsulation can be implemented using Class and access
modifiers
Example:
#include<iostream>
class Circle {
private:
double radius;
public:
void compute_area(double r) {
// member function can access private
radius = r;
};
// main function
int main() {
Circle obj;
obj.compute_area(1.5);
return 0;
Constructors
A constructor is a special type of member function of a class which
initializes objects of a class. In C++, Constructor is automatically called
when object(instance of class) create. It is special member function of
the class because it does not have any return type.
Example:
#include <iostream>
using namespace std;
class point {
private:
double x, y;
public:
// Non-default Constructor &
// default Constructor
point (double px, double py) {
x = px, y = py;
}
};
int main(void) {
// Define an array of size
// 10 & of type point
// This line will cause error
point a[10];
// Remove above line and program
// will compile without error
point b = point(5, 6);
}
Distructors
Example:
class String {
private:
char* s;
int size;
public:
String(char*); // constructor
~String(); // destructor
};
String::String(char* c) {
size = strlen(c);
s = new char[size + 1];
strcpy(s, c);
}
String::~String() {
delete[] s;
}
Polymorphism
The word polymorphism means having many forms. In simple words,
we can define polymorphism as the ability of a message to be
displayed in more than one form. A real-life example of polymorphism,
a person at the same time can have different characteristics. Like a
man at the same time is a father, a husband, an employee. So the
same person posses different behavior in different situations. This is
called polymorphism. Polymorphism is considered as one of the
important features of Object Oriented Programming.
Example:
#include<iostream>
class Complex {
private:
public:
real = r;
imag = i;
}
// This is automatically called when '+' is used with
Complex res;
return res;
void print() {
cout << real << " + i" << imag << endl;
};
int main() {
Complex c1(10, 5), c2(2, 4);
c3.print();
class base {
public:
virtual void print () {
cout<< "print base class" <<endl;
}
void show () {
cout<< "show base class" <<endl;
}
};
void show () {
cout<< "show derived class" <<endl;
}
};
//main function
int main() {
base *bptr;
derived d;
bptr = &d;
return 0;
}
Defining functions outside the class
The member function can be declared outside the class using scope
resolution operator ( :: )
Example:
#include<iostream>
#include<conio.h>
class math {
private:
int a,b,c;
public:
void sum();
};
void math::sum() {
cin>>a>>b;
c=a+b;
int main() {
s.sum();
getch();
return 0;
Friend function
In object-oriented programming, a friend function, that is a "friend" of
a given class, is a function that is given the same access as methods to
private and protected data. [1]
A friend function is declared by the class that is granting access, so
friend functions are part of the class interface, like methods. Friend
functions allow alternative syntax to use objects, for
instance f(x) instead of x.f() , or g(x,y) instead of x.g(y) .
Friend functions have the same implications on encapsulation as
methods.
Example:
#include <iostream>
class A {
private:
int a;
public:
A() {
a = 0;
};
class B {
private:
int b;
public:
void showA(A& x) {
// private members of A
};
int main() {
A a;
B b;
b.showA(a);
return 0;
}
Inheritance
The capability of a class to derive properties and characteristics from
another class is called Inheritance. Inheritance is one of the most
important feature of Object Oriented Programming.
Sub Class: The class that inherits properties from another class is
called Sub class or Derived Class.
Super Class:The class whose properties are inherited by sub class is
called Base Class or Super class.
Types of Inheritance in C++
Example:
#include <iostream>
// base class
class Vehicle {
public:
Vehicle() {
};
// sub class derived from a single base classes
};
// main function
int main() {
Car obj;
return 0;
Example:
#include <iostream>
class Vehicle {
public:
Vehicle() {
};
class FourWheeler {
public:
FourWheeler() {
};
};
// main function
int main() {
Car obj;
return 0;
}
3.Multilevel Inheritance: In this type of inheritance, a derived class is
created from another derived class.
Example:
#include <iostream>
using namespace std;
// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
Example:
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle" << endl;
}
};
};
};
// main function
int main() {
// creating object of sub class will
// invoke the constructor of base class
Car obj1;
Bus obj2;
return 0;
}
5.Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by
combining more than one type of inheritance. For example: Combining
Hierarchical inheritance and Multiple Inheritance.
Example:
#include <iostream>
// base class
class Vehicle {
public:
Vehicle() {
};
//base class
class Fare {
public:
Fare() {
cout<<"Fare of Vehicle\n";
};
};
};
// main function
int main() {
Bus obj2;
return 0;
}
Exception Handling
One of the advantages of C++ over C is Exception Handling.
Exceptions are run-time anomalies or abnormal conditions that a
program encounters during its execution. There are two types of
exceptions: a)Synchronous, b)Asynchronous(Ex:which are beyond the
program’s control, Disc failure etc). C++ provides following specialized
keywords for this purpose.
try: represents a block of code that can throw an exception.
catch: represents a block of code that is executed when a particular
exception is thrown.
throw: Used to throw an exception. Also used to list the exceptions that
a function throws, but doesn’t handle itself.
Example:
#include <iostream>
int main() {
int x = -1;
// Some code
try {
if (x < 0) {
throw x;
} catch (int x ) {
return 0;
File Handling
In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream
available in fstream headerfile.
ofstream:
Now the first step to open the particular file for read or write
operation. We can open file by
ifstream fin(“filename”);
ifstream fin;
fin.open(filename, openmode)
fin.open(“filename”);
Modes :
ate at end The output position starts at the end of the file.
app Append All output operations happen at the end of the file,
appending to its existing contents.
trunc Truncate Any contents that existed in the file before it is open
are discarded.
ifstream ios::in
ofstream ios::out
Problem Statement :
Examples:
Input :
-1
Output :
IDE
• C++
/* File Handling with C++ using ifstream & ofstream class object*/
/* To write the Content in File*/
#include <iostream>
fstream classes */
#include <fstream>
// Driver Code
int main() {
ofstream fout;
string line;
// fout.open("sample.txt", ios::app)
fout.open("sample.txt");
getline(cin, line);
// Press -1 to exit
if (line == "-1")
break;
fout.close();
ifstream fin;
fin.open("sample.txt");
// Execute a loop until EOF (End of File)
while (fin) {
getline(fin, line);
fin.close();
return 0;