Class: It is a user-defined datatype enclosed with variables and functions. It is like a blueprint for an object. Class members are private by default. For Example, the car is an object, its color, design, weight are its attributes whereas the brake, speed limit, etc. are its functions.
Syntax:
class <class_name>
{
private:
// Data members
........... ...... .......
public:
// Data members
// Member function
......... ........ ......
protected:
// Data members
// Member function
....... ....... ......
};
Below is the C++ program to illustrate the class:
C++
#include <bits/stdc++.h>
using namespace std;
class Geeks {
public :
string geekname;
int roll_no;
void printName()
{
cout << "Geekname is: "
<< geekname;
}
void printRollno()
{
cout << "Roll no is: "
<< roll_no;
}
};
int main()
{
Geeks obj1;
obj1.geekname = "Geek" ;
obj1.roll_no = 15;
obj1.printName();
cout << endl;
obj1.printRollno();
return 0;
}
|
Output:
Geekname is: Geek
Roll no is: 15
Explanation: In the above example of C++ class Geeks has data members and member function in public access specifier. The data members are initialized with some values using the object of the class. Using the object, the functions of the class are being called.
Structure: It is a convenient way for grouping variables. Structure members are public members by default. Structures can be used to effectively raise the level of abstraction in order to improve the code.
Syntax:
struct <struct_name>
{
// Data members
....... ...... ......
// Member functions
....... ...... ......
};
Below is the C++ program to illustrate structure:
C++
#include <bits/stdc++.h>
using namespace std;
struct geekyPerson {
string name;
int age;
float salary;
void display()
{
cout << "Name: " << name
<< endl;
cout << "Age: " << age
<< endl;
cout << "Salary: " << salary;
}
};
int main()
{
geekyPerson g1;
g1.name = "Geeky" ;
g1.age = 23;
g1.salary = 15000;
g1.display();
return 0;
}
|
Output:
Name: Geeky
Age: 23
Salary: 15000
Explanation: The struct tag is used before the structure name to define a structure. Each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. Using the object, the functions of the structure are being called.
Union: It is a user-defined datatype. It is similar to structures. All the data members of the union share the same memory location. The size of the union is based on the size of the largest member of the union. To use the same memory location for two or more members, a union will be the best.
Syntax:
union <union_name>
{
// Data members
..... ....... .........
} union_variables;
where,
union_name − Name was given to the union.
union_variable − The object of union.
Data members -Member variables.
Below is the C++ program to illustrate union:
C++
#include <bits/stdc++.h>
using namespace std;
union geeks {
int id;
int salary;
char name[30];
int age;
};
int main()
{
union geeks g1;
g1.id = 1;
cout << "Id : " << g1.id
<< endl;
strcpy (g1.name, "Geeky" );
cout << "Name : " << g1.name
<< endl;
g1.salary = 35000;
cout << "Salary : " << g1.salary
<< endl;
g1.age = 25;
cout << "Age : " << g1.age
<< endl;
return 0;
}
|
Output:
Id : 1
Name : Geeky
Salary : 35000
Age : 25
Explanation: In union also, objects are used to access the data members and member functions.