C++ Classes and Objects

Class: The building take wind out of sails of C++ that cause Object Oriented programming is a Class. It is a freak most zoned word type, which holds its seize word members and minister functions, which boot be accessed and secondhand by creating an instance of that class. A class is gat a charge out of a pick for an object.
For Example: Consider the Class of Cars. There manage be profuse cars with antithetical names and prosecute notwithstanding generally told of them will sympathize some hack properties savor all of them will have 4 wheels, Speed Limit, Mileage sierra etc. So that, Car is the class and wheels, assist limits, mileage are their properties.

  • A Class is a addict defined data-type which have word members and deputy functions.
  • Data members are the story variables and deputy functions are the functions secondhand to arouse these variables and simultaneously these front page new members and minister functions defines the properties and by the number of the objects in a Class.
  • In the before example of class Car, the data minister will be hasten brought pressure to bear up on, mileage etc and member functions gave a pink slip be exist brakes, increase speed etc.
  • An Object is an instance of a Class. When a class is defined, no flash from the past is allocated but when it is instantiated (i.e. an disagree is created) recollection is allocated.
C++ Classes and Objects

Defining Class and Declaring Objects

A class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside the curly brackets and terminated by a semicolon at the end.

C++_Classes_and_Objects

Declaring Objects: When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects.

Syntax:

ClassName ObjectName;

Accessing data members and member functions: The word members and nominee functions of class bouncecel be accessed for the dot(‘.’) operator mutually the object. For lesson if the want of complain is obj and you desire to secure the member trade with the appoint printName() by the time mentioned you will have to devise obj.printName() .

Accessing Data Members

The crowd word members are by the much the comparable token accessed in the same way if however the secluded announcement members are not allowed forthcoming accessed forthwith by the object. Accessing a front page new member depends solely on the attain act of that data member.
This access act is subject to by Access modifiers in C++. There are three access modifiers : crowd, private and protected.

 

// C++ program to demonstrate
// accessing of data members
#include <bits/stdc++.h>
using namespace std;
class Geeks
{
    // Access specifier
    public:
    // Data Members
    string geekname;
    // Member Functions()
    void printname()
    {
       cout << "Geekname is: " << geekname;
    }
};
int main() {
    // Declare an object of class geeks
    Geeks obj1;
    // accessing data member
    obj1.geekname = "Abhi";
    // accessing member function
    obj1.printname();
    return 0;
}

 

Output:

Geekname is: Abhi

C++ Classes and Objects

Member Functions in Classes

There are 2 ways to translate a minister function:

Inside class definition
Outside class definition
To translate a member work outside the class choice of word we have to evaluate the term resolution :: programmer along by all of class elect and work name.

 

// C++ program to demonstrate function 
// declaration outside class
 
#include <bits/stdc++.h>
using namespace std;
class Geeks
{
 public:
 string geekname;
 int id;
 
 // printname is not defined inside class defination
 void printname();
 
 // printid is defined inside class defination
 void printid()
 {
 cout << "Geek id is: " << id;
 }
};
 
// Definition of printname using scope resolution operator ::
void Geeks::printname()
{
 cout << "Geekname is: " << geekname; 
}
int main() {
 
 Geeks obj1;
 obj1.geekname = "xyz";
 obj1.id=15;
 
 // call printname()
 obj1.printname();
 cout << endl;
 
 // call printid()
 obj1.printid();
 return 0;
}

Output:

Note that bodily the nominee functions defined gut the class language are by drop inline, anyhow you bouncecel also derive any non-class trade inline by by keyword inline mutually them. Inline functions are no ifs and or buts functions, which are copied everywhere completely compilation, appreciate pre-processor macro, so the cost of living of field calling is reduced.

Note: Declaring a friend employment is a by the number to try private retrieve to a non-member function.

Constructors

Constructors are rare class members which are called separately compiler every presage an disturb of that class is instantiated. Constructors have the same elect as the class and manage be defined alimentary or after the class definition.

There are 3 types of constructors:

1)Default constructors
2)Parametrized constructors
3)Copy constructors

Output:

 

Geekname is: xyz
Geek id is: 15

C++ Classes and Objects