2-C++-Intro to class copy
2-C++-Intro to class copy
Inline Functions
Each time a function is called, a significant amount of
overhead is generated by the calling and return mechanism.
1
In C++ the function code can be expanded at the point where it
is called, termed as inline functions.
2
Like the register keyword, inline is actually just a request,
not a command, to the compiler. The compiler can choose to
ignore it. If a function cannot be inlined, it will simply be
called as a normal function.
3
The general form of a simple class declaration is
class class-name {
private:
private data and functions
public:
public data and functions
};
int main( ) {
complex c1;
c1.accept( );
c1.display( ); }
4
No data-member can be declared as auto, extern or register.
# include <iostream>
using namespace std;
class complex
{
private: int r, i;
public:
void accept( );
void display( ); // Declaration of member functions
};
5
void complex::accept( )
{
cin>>r>>i;
}
void complex::display( )
{
cout<<r<<"+i"<<i;
}
int main( ) {
complex c1;
c1.accept( );
c1.display( ); }
int main( ) {
student s1;
s1.Assign("abc","145","10 8 9") // s1.Accept( );
s1.Print();
}
‘this’ pointer
Member functions are part of the class. Hence, they can
access private data-members.
8
Program to demonstrate ‘this’ holds the address of the
invoking object.
# include <iostream>
using namespace std;
class complex
{
private: int r, i;
public:
void access( );
};
void complex::access( )
{
cout<<"\nContent of \"this\" is "<<this<<endl;
this->r = 10;
cout<<this->r;
}
int main( ) {
complex c1;
cout<<"Address of c1 is "<<&c1;
c1.access( );
}