C Sharp Tutorial
C Sharp Tutorial
C# Tutorials
Objects:
This is the basic unit of object oriented programming. That is both data and function that
operate on data are bundled as a unit called as object.
Classes:
The concept of class is similar to the concept of structure in C. In other words classes are
the data types on which objects are created. So while a class is created no memory is
allocated only when an object is created memory gets allocated.
Inheritance:
As the name suggests Inheritance is the process of forming a new class from an existing
class that is from the existing class called as base class, new class is formed called as
derived class. This is a very important concept of object oriented programming since this
feature helps to reduce the code size.
Data Abstraction:
Data Encapsulation:
Data Encapsulation is the process of combining data and functions into a single unit
called class. By this method one cannot access the data directly. Data is accessible only
through the functions present inside the class. Thus Data Encapsulation gave rise to the
important concept of data hiding.
Polymorphism:
The ability to use an operator or function in different ways in other words giving different
meaning or functions to the operators or functions is called polymorphism. Poly refers
many. That is a single function or an operator functioning in many ways different upon
the usage is called polymorphism.
What is an Object?
An object is a software bundle of related state and behavior.
Software objects are often used to model the real-world objects that you find in everyday
life. Real-world objects share two characteristics: They all have state and behavior.
Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging
tail).
SUNSAT The Perfect Team
C Sharp Tutorial
Software objects are conceptually similar to real-world objects:
they too consist of state and related behavior.
An object stores its state in fields (variables in some programming languages)
and exposes its behavior through methods (functions in some programming languages).
Methods operate on an object’s internal state and serve as the primary mechanism for
object-to-object communication.
Hiding internal state and requiring all interaction to be performed through an object’s
methods is known as
data encapsulation — a fundamental principle of object-oriented programming
What Is a Class?
A class is a blueprint or prototype from which objects are created.
What Is Inheritance?
Inheritance provides a powerful and natural mechanism for organizing and structuring
your software.
What Is an Interface?
An interface is a contract between a class and the outside world.
When a class implements an interface, it promises to provide the behavior published by
that interface.
What Is a Package?
A package is a namespace for organizing classes and interfaces in a logical manner.
Placing your code into packages makes large software projects easier to manage.
Overloading:
The concept of overloading is also a branch of polymorphism. When the exiting operator
or function is made to operate on new data type it is said to be overloaded.
Reusability:
That is object oriented programming has the feature of allowing an existing class which is
written and debugged to be used by other programmers and there by provides a great time
saving and also code efficiency to the language. Also it is possible to a have the existing
class and adds new features to the existing class as pet the programmer’s choice.
Thus the object oriented programming features helps the program ad there by users of the
application to achieve increased performance, it saves time of developing the application,
give optimized code for the application, helps in gaining secured applications and there
by helps in easier maintenance.
We will learn how to implement each of these feature of object oriented programming in
C++ later in later sections.
SUNSAT The Perfect Team
C Sharp Tutorial
Basic Structure:
To start with the programming language C++ let us see how a have an overlook of basic
structure of C++ program.
Let us see a sample program to have an understanding of the basic structure of C++
Though the later section would explain each section in detail let us see overlook of the
structure of the above program.
Class:
A class in C++ is an encapsulation of data members and functions that manipulate the
data. The class can also have some other important members which are architecturally
important.
This C++ Tutorial discusses the components of a C++ class. More C++ tutorials will
follow.
The data members can be of any legal data type, a class type, a struct type etc., They can
also be declared as pointers and accessible normally as like other data members. The
Example class given below in this C++ tutorial has two data members x and y of type
integer.
Functions declared inside a class can be any of the following four types. This C++
Tutorial explains each one of them as below.
These are ordinary functions defined with a return type and parameters. The return type
can also be void. The special trait about member functions is they can access the
private/protected data members of their class and manipulate them. No external functions
can access the private/protected data members of a class. The sample below this C++
Tutorial uses an ordinary member function Add(), returning an integer value.
Constructors:
Constructors in C++ are special member functions of a class. They have the same name
as the Class Name. There can be any number of overloaded constructors inside a class,
provided they have a different set of parameters. There are some important qualities for a
constructor to be noted.
In the example class given below in this C++ tutorial has the constructor
Example_Class(), with the same name as the class.
Destructors:
Destructors in C++ also have the same name, except for the fact that they are preceded
by a ‘~’ operator. The destructors are called when the object of a class goes out of scope.
It is not necessary to declare a constructor or a destructor inside a class. If not declared,
the compiler will automatically create a default one for each. If the constructor/destructor
is declared as private, then the class cannot be instantiated. Check below for the sample
class of the C++ tutorial for an example of destructor.
Private:
The members are accessible only by the member functions or friend functions.
Protected:
These members are accessible by the member functions of the class and the classes
which are derived from this class.
Public:
Inheritence
Creating or deriving a new class using another class as a base is called inheritance in
C++. The new class created is called a Derived class and the old class used as a base is
called a Base class in C++ inheritance terminology.
The derived class will inherit all the features of the base class in C++ inheritance. The
derived class can also add its own features, data etc., It can also override some of the
features (functions) of the base class, if the function is declared as virtual in base class.
There are some points to be remembered about C++ inheritance. The protected and
public variables or members of the base class are all accessible in the derived class. But a
private member variable not accessible by a derived class.
It is a well known fact that the private and protected members are not accessible outside
the class. But a derived class is given access to protected members of the base class.
Let us see a piece of sample code for C++ inheritance. The sample code considers a class
named vehicle with two properties to it, namely color and the number of wheels. A
vehicle is a generic term and it can later be extended to any moving vehicles like car,
bike, bus etc.,
class Car: public vehicle //Sample derived class for C++ inheritance tutorial
{
protected:
char type_of_fuel;
public:
Car();
};
The derived class Car will have access to the protected members of the base class. It can
also use the functions start, stop and run provided the functionalities remain the same.
In case the derived class needs some different functionalities for the same functions start,
stop and run, then the base class should implement the concept of virtual functions.
The asterisk in the above specifies that we have a pointer variable. Let’s say we want to
define an int variable and then we want to define a pointer variable, which will store the
memory address of this int:
signed main()
{
int myval(7);
int* p_myval;
//Right now, p_myval contains no particular myval.
p_myval = &myval;
//Now, p_myval in this c++ program contains the memory address of the variable myval
}
With &myval, & is referred to as "the address-of operator". The expression &myval is of
the c++ type int*. We then store this int* myval in our int* variable, which is p_myval.
Now, we will actually use this pointer:
signed main()
{
int myval = 7;
int* p_myval = &myval;
*p_myval = 6;
}
With *p_myval = 6, the asterisk is referred to as "the dereference operator". It turns the
expression from an int* into an int. The statement has the effect of setting the myval of
myval to 6. So now what are the uses of pointers in c++? Let us see something about how
and when they should be used:
Here’s what the string (char c++ pointer) looks like in memory:
int main()
{
char my_name[] = "Code";
char* k( &my_name[1] );
}
signed main()
{
signed** p_p_cow;
}
An int* c++ pointer points to an int, so an int** points to an int*. In English: The
variable p_cow above stores a memory address. At that memory address exists a variable
of type int*. This int* variable also stores a memory address, at which exists an int. Take
the following:
int main()
{
int cow(7);
int* p_cow = &cow;
int** p_p_cow(&p_cow);
int*** p_p_p_cow = &p_p_cow;
}
int main()
{
int cow(7);
int* p_cow = &cow;
int** p_p_cow(&p_cow);
int*** p_p_p_cow = &p_p_cow;
***p_p_p_cow = 8;
}
Or we can define a function, which will be supplied with a string. The function will
return the first instance of the character ‘t’ in the string:
for ( ; *p ; ++p)
{
if ( *p == ‘t’ ) return p;
}
return 0;
signed main()
{
Now I’m going to talk about c++ pointers and constness. If you want a const c++ pointer
variable, a c++ pointer variable whose value you can’t change after initialization, then
stick the const directly beside the variable’s name:
signed main()
{
int myval = 5;
int myvalue2(8);
int* const p_k = &myval;
p_k = &myvalue2; //COMPILE ERROR
*p_k = 3; //No problem, the variable to which it points is non-const
signed main()
{
int myval(7);
int myvalue2 = 6;
const int* p_k = &myval;
p_k = &myvalue2; //No problem, the variable is non-const
*p_k = 7; //COMPILE ERROR
If you want a const c++ pointer that points to a const variable then:
int myval(17);
int myvalue2 = 4;
const int* const p_k = &myval;
p_k = &myvalue2; //COMPILE ERROR
*p_k = 32; //COMPILE ERROR