C++ Unit-2 2
C++ Unit-2 2
Unit-II
Variable
Array and Strings
Classes member functions
Friend function
Inline function
Stored Data
• There are
– predefined data types
– system-defined types
– user-defined types
About Data Types contd.
• Predefined data types are part of the C++ language definition.
– Examples: float, double - real. int - integer. char
• We denote char literals with single quotes, for example: ‘A’ ‘*’ ‘2’
– A string literal is a sequence of characters in double quotes:
• “ABCDE”
• “127” (not the same as int 127)
• “true” (not the same as bool true)
•
•System-defined types - part of the C++ class libraries. Not part of the original
C++ language definition but added when the compiler is written.
– The standard I/O stream objects cin and cout are defined in iostream
library
– Also there is a string class (type) and classes for input and output files.
• To declare an output file: ofstream cprint (“file.txt”);
•
• User-defined types - e.g., enum type, classes
Declarations
•Declarations inform the compiler that it will need to set aside space in
memory to hold an object of a particular type (class) with a particular
name.
•
• Constant declarations
– Used to associate meaningful names with constants -- items that
will never change throughout the execution of the program.
– One convention is to use all uppercase letters for constant
identifiers.
int main() {
return 0;
} 10
What Is An Array?
• An array is a collection of values that have the same data type, e.g.
– A collection of int data values or
– A collection of bool data values
• Structures are value types and the classes are reference types.
• cout is used together with the insertion operator, which is written as <<
• The << operator inserts the data that follows it into the stream that
precedes it.
• Ex
cout << "Output sentence"; // prints Output sentence on screen
cout << 120; // prints number 120 on screen
cout << x; // prints the value of x on screen
cout << "Hello"; // prints Hello
cout << Hello; // prints the content of variable Hello
Standard input
• the standard input by default is the keyboard, and the C++ stream object
defined to access it is cin.
• cin is used together with the extraction operator, which is written as >>
• The >> operator is then followed by the variable where the extracted data
is stored
• Ex:
int age, a, b;
cin >> age; // inputs a number of type int and stores it in age
cin>>a>>b ; // input two numbers ot type int and saves it in a &
Structure of C++ program
• Main Function
– Each and every C++ program always starts with main function.
– This is entry point for all the function. Each and every method is called
indirectly through main.
– We can create class objects in the main.
– Operating system call this function automatically.
Class Declaration
• As we learned earlier
• Class is a way to bind the data and its associated functions together
class class-name
{
access specifier:
data and functions
}
• defines the access rights for the statements or functions that follows
it until another access specifier or till the end of a class. The three
types of access specifiers are "private", "public", "protected".
• Each object will have its own instances of class data and functions
Creating objects
st st
1 2
55, Mary
void read_data( )
void print_data( )
st
Accessing Data Members & functions
• To access data members of class we use dot operator
• Can be used to
1. To access a global variable when there is a local variable with same
name
2. To define a function outside a class.
3. To access a class’s static variables.
4. to qualify hidden names
SCOPE RESOLUTION OPERATOR
• Write a C++ program to demonstrate concept of scope resolution operator?
#include<iostream.h>
#include<conio.h>
int num=20;
void main()
{
int num=10;
clrscr();
cout<<"local="<<num;
cout<<endl<<"global="<<:: num;
cout<<"global+local="<<::num+num;
}
Output:local=10
Global=20
Global+local=30
Member functions
• Member functions can be defined
– within the class definition or
– separately using scope resolution operator, ::
• If we define the function inside class then we don't not need to declare it
first.
Member functions
• to define the member function outside the class definition
– we must declare the function inside class definition and then define it
outside.
• we have to use the scope resolution :: operator along with class name
along with function name
Syntax
• return_type class_name::function_name() {…}
• The main function for both the function definition will be same.
Friend Functions
• Friend function is a non-member function which can access the private
& protected members of a class”.
Syntax:
class class_name
{
//class definition
public:
friend returntype fun_name(formal parameters);
};
Friend function
#include<iostream.h>
class base
{
int val1, val2;
public:
void get()
{
cout<<”enter two values”<<endl;
cin>>val1>>val2;
}
friend float mean(base ob);
};
float mean(base ob)
{
return float((ob.val1+ob.val2)/2);
}
int main()
{
base obj;
obj.get();
cout<<”Mean Value is:”<<mean(obj);
return 0;
}
• Friend class and all of its member functions have access to the private
members defined within the that class.
• When a class is made a friend class, all the member functions of that
class becomes friend functions.
Friend class
• Ex
... .. ...
class B;
class A {
// class B is a friend class of class A
friend class B;
... .. ...
};
class B
{ ... ..// Now class B can access all members of A... };
Inline function
#include<iostream.h>
#include<conio.h>
int num=20;
void main()
{
int num=10;
clrscr();
cout<<"local="<<num;
cout<<endl<<"global="<<:: num;
cout<<"global+local="<<::num+num;
getch();
}
Output:local=10
Global=20
Global+local=30
This pointer
• The this pointer points to the object that invoked the function
• Dynamic objects are objects that are created / Instantiated at the run
time by the class”.
• They are Live Objects, initialized with necessary data at run time.
• We can allocate memory at run time within the heap using new operator
Syntax
• new data-type;
– data-type could be any built-in data type including an array or any
user defined data types include class or structure
Delete Operator
• Syntax
• Delete pointer;