CPlus Plus
CPlus Plus
int main()
{
char name[100];
cout << "Enter your name: ";
cin >> name; //scanf
cout << "Hello " <<name<<endl; //printf
return 0;
}
C++ has two types of data types:
• One-dimensional Array
• Two –dimensional Array
Structure
- A linear, direct access data structure with
heterogeneous components
- A component is also called a field or a member
- Each field has its own type
- An identifier is used to name a field/component
Example:
struct CarType {
int year;
char maker[10];
float price;
};
CarType MyCar;//in C++ CarType is considered a data type
//name; unlike C where you have to write struct CarType
Class and Object
• Class is a data type that encapsulates a fixed number of data
components with the functions that can access and/or manipulate them
• It gives the C++ its identity from C
• It makes possible encapsulation, data hiding and inheritance
• Consists of both data and methods
• Defines properties and behavior of a set of entities
• Abstract concept: liken to the specification of a laptop
• Object
• An instance of a class
• A variable identified by a unique name
• Concrete concept: liken to an actual laptop having a given specification
Objects
Object:
a variable or an instance of a class
OBJECT
set of methods
Operations (public member functions)
Data
internal state
(values of private data members)
Motivation for classes
• Object-Oriented Programming (OOP)
• Package together a set of related data and operations
(encapsulation)
• Allows programmer to define a class (abstract data type)
• One instance (variable) of a class is called an object
• The data and operations of a class are called its
members.
A Class & Its Objects
class Rectangle Rectangle r1;
{ Rectangle r2;
Rectangle r3;
private://by default
int width;
int length;
public:
void set(int w, int l);
int area();
};
Define a Class Type
class Rectangle
class class_name {
{ private:
permission_label:
int width;
member;
permission_label: int length;
member; public:
... void set(int w, int l);
};
int area();
};
Another Example //Circle.cpp file
//Circle.h file // member function definitions
#include <iostream.h> circle::circle(){radius = 0.0;}
void display (double r){
class circle cout << r << endl;
{ }
private:
double radius; void circle:: store(double r){
radius = r;
public: }
circle(); double circle::area(){
void store(double); return 3.14*radius*radius;
double area(); }
void display(); void circle::display(){
}; cout << “r = “ << radius << endl;
void display(double r); }
//main.cpp file
int main(void) {
circle c; // an object of circle class
c.store(5.0); cout << "The area of circle c is " << c.area() << endl;
c.display();
}
Class Definition - Access Control
• Information hiding
• To prevent the internal representation from direct access from
outside the class
• Access Specifiers
• public
• may be accessible from anywhere within a program
• private
• may be accessed only by the member functions, and friends
of this class, not accessible to nonmember functions
• Protected
• Accessible only to derived/child + same class member
functions
• Difference between classes and structs in C++
the default access specifier is private in class
the default access specifier is public in struct
Class Definition – Member Functions
• Used to
• access the values of the data members (accessor/get method)
• perform operations on the data members (modifier/set method)
• Are declared inside the class body, in the same way as
declaring a function
• Their definition can be placed inside the class body, or
outside the class body
• Can access both public and private members of the class
• Can be called using dot (for an object) or arrow (for a
pointer to an object) member access operator
Define a Member Function
class Rectangle
{
private:
int width, length;
public:
void set (int w, int l){width=w;length=l;}
int area() {return width*length; }
}
Rectangle r1;
r1.set(5,8);
Rectangle *rp=&r1;
rp -> set(8,10);//equivalent to: r1.set(8,10); or (*rp).set(8,10);
Defining Methods Separately
• For methods/functions that are declared but not defined in the class we
need to provide a separate definition
• To define the method, you define it as any other function, except that
the name of the function is written as:-
ClassName::FuncName
:: is the scope resolution operator, it allows us to refer to parts of a class or
structure
Defining Member Functions
class Rectangle
{
private:
int width, length; class name
public:
void set (int w, int l);
member function name
int area() {return width*length; }
}
class Rectangle
{
private:
int width, length;
public:
void set (int w, int l);
int area() {return width*length; }
}
struct A{
int *i;
A (int k) {
i = new int;
*i = k;
cout << “ctor ” << *i << '\n';
}
~A() {cout << “dtor ” << *i << '\n'; delete i;} //destructor
};
int main(){
A a1(1);//constructor is called to instantiate a1
if(1){ // nested scope
A a2(2); //constructor is called to instantiate a2
} // a2 out of scope; so destructor is called on a2
} // a1 out of scope; so destructor is called on a1
Destructor Example Output
ctor a1
ctor a2
dtor a2
dtor a1
Basics
• Header files
• Separate files in which class definitions are placed.
• Allow compiler to recognize the classes when used elsewhere.
• Generally have .h filename extensions
• Driver file
• A program used to test software (such as classes).
• Contains a main function so it can be executed
Basics
#ifndef DYNARR_H_INCLUDED
#define DYNARR_H_INCLUDED
class dynArr
{
private:
int *data;
int size;
public:
dynArr();
dynArr(int);
~dynArr();
void allocate(int);
void setValue(int, int);
int getValue(int);
};
#endif // DYNARR_H_INCLUDED
int main()
{
int n;
cin>>n;
dynArr d(n);
int i;
for(i=0;i<n;i++)
d. setValue(i,3*i+1);
for(i=0;i<n;i++)
cout << d.getValue(i) << endl;
d.allocate(100);//reallocate space
for(i=0;i<n;i++)
cout << d.getValue(i) << endl; //prints garbage values
return 0;
}
main.cpp (driver file)
Template Class
• Now we have a neat class that gives us a 1D dynamic array (you are free to make
your own improvisations at home by adding more functions to the class)
• But it only works for integer type
• What if we are to make it versatile, so that it works for any type, e.g. float,
double and char
• Should we have separate classes for each type?
• Write the same code for each type with just minor changes?
• Instead, we can use template classes
Template Class
#ifndef DYNARR_H_INCLUDED
#define DYNARR_H_INCLUDED
public:
dynArr();
dynArr(int);
~dynArr();
void allocate(int);
void setValue(int, T);
T getValue(int);
};
#endif // DYNARR_H_INCLUDED
dynarr.h (declaration file)
Template Class
#include "dynarr.h" template <class T>
#include <iostream> void dynArr<T>::allocate(int s)
using namespace std; {
if(data!=NULL) delete [] data;
template <class T> data = new T[s];
dynArr<T>::dynArr() size = s;
{
}
data = NULL;
size = 0;
} template <class T>
T dynArr<T>::getValue(int index)
template <class T> {
dynArr<T>::dynArr(int s) return data[index];
{ }
data = new T[s];
size = s;
template <class T>
}
void dynArr<T>::setValue(int index, T
template <class T> value)
dynArr<T>::~dynArr() {
{ data[index] = value;
delete [] data; }
} dynarr.cpp (definition file)
Template Class
#include "dynarr.cpp"
#include <iostream>
using namespace std;
int main()
{
dynArr<int> di(10);
dynArr<double> dd(10);
int i;
for(i=0;i<10;i++)
{
di.setValue(i,3*i+1);
dd.setValue(i,7.29*i/1.45);
}
for(i=0;i<10;i++)
cout << di.getValue(i) << " " << dd.getValue(i) << endl;
return 0;
}
main.cpp (driver file)
A Complex Number Example
class Complex
{ Complex Complex::Add(Complex c)
public: {
Complex(); Complex t;
Complex(double, double); t.Real = Real + c.Real;
Complex Add(Complex); t.Imaginary = Imaginary + c.Imaginary;
void Print(); return t;
private: }
double Real, Imaginary; void Complex::Print()
}; {
cout << Real << endl;
Complex::Complex() cout << Imaginary << endl;
{ }
Real = 0;
Imaginary = 0;
} int main(void)
{
Complex::Complex(double r, double i) Complex A(1,1), B(2,3);
{ Complex C;
Real = r; C = A.Add(B);
Imaginary = i; C.Print();
} return 0;
}
Friend Function
Complex::Complex()
{
Real = 0;
Imaginary = 0;
}
Complex::Complex(double r, double i)
{
Real = r;
Imaginary = i;
}
Friend Function
void Complex::Print()
{
cout << Real << endl;
cout << Imaginary << endl;
}
int main(void)
{
Complex A(1,1), B(2,3);
Complex C;
C = AddComplex(A, B);
C.Print();
return 0;
}
Operator Overloading
• We can use some operator symbols to define special member functions of a class
Complex::Complex()
{
Real = 0;
Imaginary = 0;
}
Complex::Complex(double r, double i)
{
Real = r;
Imaginary = i;
}
Operator Overloading
Complex Complex::operator+(Complex a){
Complex t;
t.Real = Real + a.Real;
t.Imaginary = Imaginary + a.Imaginary;
return t;
}
void Complex::Print(){
cout << Real << endl;
cout << Imaginary << endl;
}
int main(void) {
Complex A(1,1), B(2,3), D(5,6), C;
C = A + B; //C = A.operator+(B);
C = C + 3.2; //C = C.operator+(3.2);
C.Print();
return 0;
}
Operator Overloading using friend
function
With simple operator overloading we can’t do:
Complex A, C = 3.2+A;
Complex::Complex()
{
Real = 0;
Imaginary = 0;
}
Complex::Complex(double r, double i)
{
Real = r;
Imaginary = i;
}
Operator Overloading with friend function
Complex operator+(double n, Complex a){
Complex t;
t.Real = n + a.Real;
t.Imaginary = n + a.Imaginary;
return t;
}
Complex operator+(Complex a, double n){
Complex t;
t.Real = n + a.Real;
t.Imaginary = n + a.Imaginary;
return t;
}
Complex operator++(Complex &a) {
a.Real ++; a.Imaginary ++;
return a;
}
void Complex::Print(){
cout << Real << endl << Imaginary << endl;
}
int main(void) {
Complex A(1,1), B;
B = 3.2 + A;//operator+(3.2, A);
A = A + 2.4;
B=++A;//operator++(A);//a=A
A.Print();B.Print();
return 0;
}
Overloading Relational Operator
class Complex
{
public:
Complex();
Complex(double, double);
bool operator>(Complex);
void Print();
private:
double Real, Imaginary;
};
Complex::Complex()
{
Real = 0;
Imaginary = 0;
}
Complex::Complex(double r, double i)
{
Real = r;
Imaginary = i;
}
Overloading Relational Operator
bool Complex::operator>(Complex a){
if(Real > a.Real || (Real == a.Real && Imaginary > a.Imaginary))
return true;
else
return false;
}
void Complex::Print(){
cout << Real << endl;cout << Imaginary << endl;
}
int main(void) {
Complex A(1,1), B;
A.Print();
B.Print();
if(A>B) //A.operator>(B);
cout<<“A is greater”;
return 0;
}