0% found this document useful (0 votes)
7 views4 pages

C++ Class Note5

The document explains C++ class methods, detailing how to define methods both inside and outside of class definitions. It includes examples demonstrating the creation of methods and constructors, highlighting their roles in initializing objects. Constructors are special methods that are automatically called when an object is created, sharing the same name as the class.

Uploaded by

barikuinwikpa5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

C++ Class Note5

The document explains C++ class methods, detailing how to define methods both inside and outside of class definitions. It includes examples demonstrating the creation of methods and constructors, highlighting their roles in initializing objects. Constructors are special methods that are automatically called when an object is created, sharing the same name as the class.

Uploaded by

barikuinwikpa5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C++

Class Methods

Methods are functions that belongs to the class.

There are two ways to define functions that belongs to a class:

 Inside class definition


 Outside class definition

Inside Example

class MyClass { // The class


public: // Access specifier
void myMethod() { // Method/function defined inside the class
cout << "Hello World!";
}
};

int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}

To define a function outside the class definition, you have to declare it inside the class
and then define it outside of the class. This is done by specifiying the name of the class,
followed the scope resolution :: operator, followed by the name of the function.

Outside Example

class MyClass { // The class


public: // Access specifier
void myMethod(); // Method/function declaration
};

// Method/function definition outside the class


void MyClass::myMethod() {
cout << "Hello World!";
}

int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}
PROGRAMS

(1)

#include <iostream>
using namespace std;

class MyClass { // The class


public: // Access specifier
void myMethod() { // Method/function
cout << "Hello World!";
}
};

int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}

(2)

#include <iostream>
using namespace std;

class MyClass { // The class


public: // Access specifier
void myMethod(); // Method/function declaration
};

// Method/function definition outside the class


void MyClass::myMethod() {
cout << "Hello World!";
}

int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}

Constructors

A constructor in C++ is a special method that is automatically called when an object of


a class is created.

Or constructor is a special method that is invoked automatically at the time an object


of a class is created. It is used to initialize the data members of new objects. The
constructor in C++ has the same name as the class or structure.
Example 1

#include <iostream>

using namespace std;

class MyClass { // The class

public: // Access specifier

MyClass() { // Constructor

cout << "Hello World!";

};

int main() {

MyClass myObj; // Create an object of MyClass (this will call the constructor)

return 0;

Example 2

#include <iostream>

using namespace std;

class A {

public:

// Constructor of the class without

// any parameters

A() {

cout << "Constructor called" << endl;

}
};

int main() {

A obj1;

return 0;

You might also like