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++ program to illustrate class #include <bits/stdc++.h> using namespace std;
// Class with data members // and member function class Geeks {
public :
string geekname;
int roll_no;
// Function printName
// prints the name
void printName()
{
cout << "Geekname is: "
<< geekname;
}
// Function printRollno
// prints the roll no
void printRollno()
{
cout << "Roll no is: "
<< roll_no;
}
}; // Driver Code int main()
{ // Object of class Geeks
Geeks obj1;
// Initialize a public data
// members of class Geeks
obj1.geekname = "Geek" ;
obj1.roll_no = 15;
// Function Call
obj1.printName();
cout << endl;
// Function Call
obj1.printRollno();
return 0;
} |
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++ program to illustrate structure #include <bits/stdc++.h> using namespace std;
// Structure with data members // and member function struct geekyPerson {
// Data members
string name;
int age;
float salary;
// Member function to display
void display()
{
cout << "Name: " << name
<< endl;
cout << "Age: " << age
<< endl;
cout << "Salary: " << salary;
}
}; // Driver Code int main()
{ // Instance of the structure
geekyPerson g1;
// Accessing the data by the
// dot operator
g1.name = "Geeky" ;
g1.age = 23;
g1.salary = 15000;
// Function Call
g1.display();
return 0;
} |
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++ program to illustrate union #include <bits/stdc++.h> using namespace std;
// Union declaration with the // data members union geeks {
int id;
int salary;
char name[30];
int age;
}; // Driver Code int main()
{ // Instance of the union
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;
} |
Id : 1 Name : Geeky Salary : 35000 Age : 25
Explanation: In union also, objects are used to access the data members and member functions.