static dataType variable;
=> Declare a static variabledataType ClassName::variable = value;
=> InitializeClassName::variable
=> Called using Class instead of Objectthis
can't be used in Static method#include "ClassName.cpp"
class ClassName {
// Properties
// Methods
};
this
=> Represents address of current Objectthis->variable
=> Access Property of Class // Static Allocation
ClassName variable;
// Dynamic Allocation
ClassName *variable = new ClassName;
ClassName *variable = new ClassName();
// For Static Allocation
var.variableName = value;
var.functionName();
// For Dynamic Allocation
(*var).variableName;
var->variableName;
(*var).functionName();
var->functionName();
ClassName variable1;
ClassName *ptr = &variable1;
ptr = NULL;
// Cannot be modified, Can invoke only const member functions
// Needs to be initialized at the time of declaration via constructors
const ClassName variable;
// Gets called when creating an Object with giving parameter
ClassName () {
variable = value;
}
// Calling It
ClassName variable;
ClassName (dataType value) {
this->var = value;
}
// Calling It, Default Constructor should exit
ClassName variable(argument);
// Default Copy constructor does Shallow copy
// If passed by value then loop of Copy constructor will be created
ClassName (ClassName &var) {
this->variable1 = var.variable1;
this->variable2 = var.variable2;
}
// Calling It
ClassName variable(objectName);
ClassName variable = objectName;
variable1 = variable2
=> Values of variable2 are copied in variable1 Objectdelete variable;
=> Calling for dynamic allocation ~ClassName() {
// Statement
}
int x;
int y;
ClassName(int variable1 = 0, int variable2 = 0):x(variable1), y(variable2) {}
// Same As
ClassName(int variable1 = 0, int variable2 = 0) {
x = variable1;
y = variable2;
}
const int variable;
ClassName(int variable):variable(variable) {}
int &variable;
ClassName(int &variable):variable(variable) {}
this
can also be usedsuper
=> Used to access methods of Parent/Base class from children class // B inherits A
class ClassNameB: accessModifier ClassNameA {
// Statements
};
// B inherits A & C inherits B
class ClassNameB: accessModifier ClassNameA {
// Statements
};
class ClassNameC: accessModifier ClassNameB {
// Statements
};
// B & C inherits A
class ClassNameB: accessModifier ClassNameA {
// Statements
};
class ClassNameC: accessModifier ClassNameA {
// Statements
};
// C inherits A & B
class ClassNameC: accessModifier ClassNameA, accessModifier ClassNameB {
// Statements
};
::
=> Scope resolution operatorvariable.A::functionName();
=> Function of Class A gets called returnType operator++() {
// Statements
}
// 1st argument is by Object, 2nd argument is passed
returnType operator+(ClassName &var) {
// Statements
}
variable1 + variable2;
// Base Class
virtual void print() {}
void show() {}
// Derived Class: Base Class
void print() {}
void show() {}
// Main
Base *ptr;
Derived var;
ptr = &var;
// Virtual function, binded at runtime
ptr->print();
// Non-virtual function, binded at compile time
ptr->show();
class ClassName {
public:
virtual returnType functionName() = 0;
}
// Base Class
virtual ~ClassName1() {}
// Derived Class
virtual ~ClassName2() {}
// Main
Derived *d = new Derived();
Base *b = d;
delete b;
returnType getVarName () {
return var;
}
void setVarName (dataType val) {
this.var = val;
}
private:
public:
protected:
friend class ClassName;
friend returnType functionName();
class ClassName1 {
public:
virtual returnType functionName() = 0;
}
class ClassName2: public CLassName1 {
public:
returnType functionName() {
// Statements
};
}
// Main
ClassName1 *ptr = new ClassName2();
ptr->show();