User defined datatypes in C++:
enumerations
structures
unions
classes
//enumeration datatype:
//c
#include <stdio.h>
int main()
{
enum DAYS {Mon,Tue,Wed,Thu,Fri,Sat,Sun};
enum DAYS d;
d = Wed;
printf("d=%d\n", d);
return 0;
}
//C++
#include <iostream>
using namespace std;
int main()
{
enum DAYS {Mon,Tue,Wed,Thu,Fri,Sat,Sun};
DAYS d;
d = Thu;
cout<<d<<endl;
return 0;
return 0;
}
enum keyword is not required while declaring variables
we should assign only enumerators, not integers for enum type variable as C++ treats enum as separate datatype internally, not as an integer datatype unlike C.
//structure datatype
//c
#include <stdio.h>
int main()
{
struct math
{
int a;
int b;
float c;
float d;
};
struct math i;
i.a = 3;
i.b = 4;
i.c = 5.6;
i.d = 7.8;
printf("i.a=%d, i.b=%d, i.c=%f, i.d=%f\n", i.a, i.b, i.c, i.d);
return 0;
}
#include <stdio.h>
struct math
{
int a;
int b;
float c;
float d;
};
int main()
{
struct math i;
i.a = 3;
i.b = 4;
i.c = 5.6;
i.d = 7.8;
printf("i.a=%d, i.b=%d, i.c=%f, i.d=%f\n", i.a, i.b, i.c, i.d);
return 0;
}
//c++
#include <stdio.h>
int main()
{
struct math
{
int a;
int b;
float c;
float d;
};
struct math i;
i.a = 3;
i.b = 4;
i.c = 5.6;
i.d = 7.8;
printf("i.a=%d, i.b=%d, i.c=%f, i.d=%f\n", i.a, i.b, i.c, i.d);
return 0;
}
#include <iostream>
using namespace std;
struct math
{
int a;
int b;
float c;
float d;
};
int main()
{
math i;
i.a = 3;
i.b = 4;
i.c = 5.6;
i.d = 7.8;
cout<<"i.a="<<i.a<<"i.b="<<i.b<<"i.c="<<i.c<<"i.d="<<i.d<<endl;
return 0;
return 0;
}
struct keyword is not required while declaring variables
#include <iostream>
using namespace std;
struct math
{
public:
int a;
int b;
int add ()
{
return a+b;
}
private:
float c;
float d;
};
int main()
{
math i;
int s;
i.a = 3;
i.b = 4;
cout<<"i.a="<<i.a<<"i.b="<<i.b<<endl;
s = i.add();
cout<<"sum="<<s<<endl;
return 0;
}
access control specifiers are there in C++: public, protected, private
function definitions can be added inside structure in C++
control specifiers:
public:
member functions can access (add())
non-member functions can access (main())
child class can access
protected:
member functions can access
non-member functions cant access
child class can access
private:
member functions can access
non-member functions cant access
child class cant access
structures in C and structures in C++
C structure members can not controlled for access using public, private and protected
C structures can not have member functions
structure vs. class in C++
structures members are by default public
class members are by default private
#include <iostream>
using namespace std;
struct math
{
int a;
int b;
int add ()
{
return a+b;
}
private:
float c;
float d;
};
int main()
{
math i;
int s;
i.a = 3;
i.b = 4;
cout<<"i.a="<<i.a<<"i.b="<<i.b<<endl;
s = i.add();
cout<<"sum="<<s<<endl;
return 0;
}
//main() can access data members a and b; and member function add() as unlabeled struct members are by default public.
#include <iostream>
using namespace std;
class math
{
int a;
int b;
int add ()
{
return a+b;
}
private:
float c;
float d;
};
int main()
{
math i;
int s;
i.a = 3;
i.b = 4;
cout<<"i.a="<<i.a<<"i.b="<<i.b<<endl;
s = i.add();
cout<<"sum="<<s<<endl;
return 0;
}
//main() CAN NOT access data members a and b; and member function add() as unlabeled class members are by default private.
//Class/structure member functions can be defined inside class/structure or outside class/structure.
#include <iostream>
using namespace std;
class math
{
public:
int a;
int b;
int add_i();
float add_f()
{
return c+d;
}
private:
float c;
float d;
};
int math::add_i()
{
return a+b;
}
int main()
{
math i;
int s;
i.a = 3;
i.b = 4;
cout<<"i.a="<<i.a<<"i.b="<<i.b<<endl;
s = i.add_i();
cout<<"sum="<<s<<endl;
return 0;
}
object oriented features: class and object
data hiding: data can be hidden in structure/class using private and protected keywords
encapsulation: binding data and functions together in class/structure
inheritance
polymorphism
#include <iostream>
using namespace std;
class STUDENT
{
public:
int rno;
int class_id;
float marks;
void dispay_profile()
{
cout<<"rno="<<rno<<", "<<"class="<<class_id<<", "<<"marks="<<marks<<endl;
}
void increment_class_id()
{
class_id++;
}
};
int main()
{
STUDENT s;
s.rno = 1;
s.class_id = 2;
s.marks = 56.98;
s.dispay_profile();
s.increment_class_id();
s.dispay_profile();
return 0;
}
//call by value - passing structures to functions
#include <iostream>
using namespace std;
struct Point
{ public:
double x, y;
};
void printPoint (Point p)
{
cout <<"(" <<p.x <<"," <<p.y <<")";
}
void offsetPoint(Point p, double nx, double ny)
{
p.x = p.x + nx;
p.y = p.y + ny;
}
int main()
{
Point p;
p.x = 3.0; p.y = 4.0;
offsetPoint(p, 1.0, 2.0);
printPoint(p);
return 0;
}
//call by reference - passing structures to functions
#include <iostream>
using namespace std;
struct Point
{ public:
double x, y;
};
void printPoint (Point p)
{
cout <<"(" <<p.x <<"," <<p.y <<")";
}
void offsetPoint(Point &p, double nx, double ny)
{
p.x = p.x + nx;
p.y = p.y + ny;
}
int main()
{
Point p;
p.x = 3.0; p.y = 4.0;
offsetPoint(p, 1.0, 2.0);
printPoint(p);
return 0;
}
//call by value- passing class to functions
#include <iostream>
using namespace std;
class Point
{ public:
double x, y;
};
void printPoint (Point p)
{
cout <<"(" <<p.x <<"," <<p.y <<")";
}
void offsetPoint(Point &p, double nx, double ny)
{
p.x = p.x + nx;
p.y = p.y + ny;
}
int main()
{
Point p;
p.x = 3.0; p.y = 4.0;
offsetPoint(p, 1.0, 2.0);
printPoint(p);
return 0;
}
//call by reference - passing class to functions
#include <iostream>
using namespace std;
class Point
{ public:
double x, y;
};
void printPoint (Point p)
{
cout <<"(" <<p.x <<"," <<p.y <<")";
}
void offsetPoint(Point &p, double nx, double ny)
{
p.x = p.x + nx;
p.y = p.y + ny;
}
int main()
{
Point p;
p.x = 3.0; p.y = 4.0;
offsetPoint(p, 1.0, 2.0);
printPoint(p);
return 0;
}
#include <iostream>
using namespace std;
class Point
{ public:
double x, y;
public:
void printPoint ()
{
cout <<"(" <<x <<"," <<y <<")";
}
void offsetPoint(double nx, double ny)
{
x = x + nx;
y = y + ny;
}
};
int main()
{
Point p;
p.x = 3.0; p.y = 4.0;
p.offsetPoint(1.0, 2.0);
p.printPoint();
return 0;
}
#include <iostream>
using namespace std;
class Point
{ public:
double x, y;
public:
void printPoint ();
void offsetPoint(double nx, double ny);
};
void Point::printPoint ()
{
cout <<"(" <<x <<"," <<y <<")";
}
void Point::offsetPoint(double nx, double ny)
{
x = x + nx;
y = y + ny;
}
int main()
{
Point p;
p.x = 3.0; p.y = 4.0;
p.offsetPoint(1.0, 2.0);
p.printPoint();
return 0;
}
#include <iostream>
using namespace std;
class Point
{
public:
double x, y;
};
class Vector
{
public:
Point start;
Point end;
void offset(double offsetX, double offsetY)
{
start.x += offsetX;
end.x += offsetX;
start.y += offsetY;
end.y += offsetY;
}
void print()
{
cout << "(" << start.x << "," << start.y << ") -> (" << end.x << "," << end.y << ")" << endl;
}
};
int main()
{
Vector vec;
vec.start.x = 3.0;
vec.start.y = 4.0;
vec.end.x = 5.0;
vec.end.y = 6.0;
vec.offset(1.0, 2.0);
vec.print();
return 0;
}