To compile a C++ script, you can use g++ -o (new name) (uncompiled name).cpp
For example, to compile a script called script.cpp:
g++ -o script script.cpp
C++ file extensions:
.cpp
.CC
.cxx
C++ header file extensions
.hpp
.h
hxx
The recommended extensions are .cpp
and .hpp
The output of the compiled code comes out as a .o
file
Preproccesed C++ Source Files have a .ii
file extension
A class in C++ is a user-defined type or data structure declared with any of the keywords class
, struct
, or union
class
is private. The private members are not accessible outside the class; they can be accessed only through member functions of the classFor example:
// Define the class class MyClass { // Class members go here };
Member functions are the functions, which have their declaration inside the class definition and works on the data members of the class. The definition of member functions can be inside or outside the definition of class.
::
operator along with class name along with function name.defined inside:
class Cube { public: int side; int getVolume() { return side*side*side; //returns volume of cube } };
defined outside:
class Cube { public: int side; int getVolume(); } int Cube :: getVolume() { return side*side*side; }
A constructor in C++ is a special ‘MEMBER FUNCTION’ having the same name as that of its class which is used to initialize some valid values to the data members of an object. It is executed automatically whenever an object of a class is created.
default constructor
class Line { public: int size; Line() { size=30; } }; ///////////// int main() { //default constructor called when object is created Line l; }
parameter constructor
class ABC { private: int x,y; public: ABC () //constructor 1 with no arguments { x = y = 0; } ABC(int a) //constructor 2 with one argument { x = y = a; } ABC(int a,int b) //constructor 3 with two argument { x = a; y = b; } }; ///////////////// int main() { ABC cc1; //constructor 1 ABC cc2(10); //constructor 2 ABC cc3(10,20); //constructor 3 return 0: }
A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete
or delete[]
. A destructor has the same name as the class and is preceded by a ~
. Example:….
class String { public: String(const char* ch); // Declare the constructor ~String(); // Declare the destructor private: char* _text{nullptr}; }; // Define the destructor. String::~String() { // Deallocate the memory that was previously reserved for the string. delete[] _text; }
A data member in C++ is a non-function member of a class (class, struct, or union).
class C { // In C++, this is a "data member". int x; };
Access controls enable you to separate the public
interface of a class from the private
implementation details and the protected
members that are only for use by derived classes.
Access control helps prevent you from using objects in ways they weren't intended to be used. This protection is lost when you make explicit type conversions.
Class members declared as public
can be used by any function.
For example:
class MyClass { public: int publicMember; };
Class members declared as private
can be used only by member functions and friends (classes or functions) of the class.
For example:
class MyClass { private: int privateMember; };
Class members declared as protected
can be used by member functions and friends (classes or functions) of the class. Additionally, they can be used by classes derived from the class.
For example:
class MyClass { protected: int protectedMember; };
In C++, an object is created from a class.
class CLASSNAME { Public: int sumthinNumb; } ///////////////////////// int main() { CLASSNAME objname; objname.sumthinNumb = 12834589028390458; cout << objname.sumthinNUmb << "/n"; return 0; } }