Swami Vivekanand International School & Jr. College: Syjc Computer Science I
Swami Vivekanand International School & Jr. College: Syjc Computer Science I
College
SYJC
Computer Science I
Chapter 3: C++
Structure Void
Array
Union
Function
Class Integral Floating Pointer
Type Type
Enumeration
C++ provides three data types which are user- defined, built – in and derived data
type.
User – defined data type provides structure, union and class while derived data type
provides arrays, function and pointer.
Built – in data type provides three types which are integral type, void and floating
type.
Integral includes integer and character while floating type includes float and double.
int x=10;
Block 2 Block 1
int x=1;
:: variable name
Eg.
int x=10;
void main()
{
int x=1;
cout<<”\nLocal X is : “<<x;
cout<<”\nGlobal X is : “<<::x;
}
Q7. Explain ‘Call by Value’ and ‘Call by Reference’ with one example of each.
Ans-
A pointer is a variable which holds the memory address of another variable.
The use of pointers in a function definition may be classifies into two groups:
Call by Value
Call by Reference
Call by Value
When a portion of the program invokes a function, control will be transferred
from the main function to the calling function and the value of actual arguments
is copied to the function.
Within function the actual value may be altered or changed.
When the control is transferred back from function to the program, altered
values are not transferred back.
This type of passing formal argument o a function is called as call by value
Eg.
void main()
{
void funct(int x,int y);
………
funct (x,y);
…….
}
void funct(int a,int b)
{
………
}
Call by Reference
In call by reference, when a function is called by a program the address of the
actual arguments are copied on to the formal arguments.
Therefore the change in value of formal argument affects the value of actual
arguments.
The content of a variable that are altered within the function are return to calling
portion of a program in the altered form.
Eg.
void main()
{
void funct(int *x,int *y);
………
………
funct(&x,&y);
……
}
void funct(int *a,int *b)
{
……
}
Q8. Explain how the memory address of a variable can be accessed in C++.
Ans-
Computer uses memory for storing the values of variables and the memory is a
sequential collection of storage cell.
Each cell has a number called address of the cell.
In C++, if declare a variable, then it gets associated with certain location where the
value of the variable is stored.
Consider, int p=30; then
p Location name(variable)
30 Value at location
7940 Location number (address)
Computer has selected 7940 memory location to store the value 30.
To access the memory address of particular variable ‘&’ operator is used.
The ‘&’ operator returns the memory address of its operand.
E.g a=&p;
Above statement assigns the memory address of variable p to the a.
This address is the location address of variable.
The operator ‘&’ is “the address of” operator.
The variable ‘a’ is declared as pointer variable as that contains the address.
The pointer variable declared in C++ asint *a; (where * indicates that a is a
pointer variable)
strlen()
This function counts number of characters in the string.
While calculating length, the function does not count the last null character.
Eg.
void main()
{
cout<<strlen(“hii”);
}
The output would be – 3
strcpy()
This function copies the content of one string into another.
Eg
void main()
{
char source[]=”Welcome”;
char target[20];
strcpy(target,source);
cout<<source<<” “<<target;
}
The output would be – Welcome Welcome
strcat()
This function concatenates the source string at the end of the target string.
Eg.
Void main()
{
char source[]=”Tom”;
char target[]=”Jerry”;
strcat(target,source);
cout<<target;
}
The output would be – TomJerry
strcmp()
This function compares two string to find out whether they are same or
different.
The two strings are compared character by character until there is mismatch or
end of one of the string is reached.
If two strings are identical, strcmp() returns value zero, otherwise it will return
numeric difference between ASCII value of non matching characters.
Eg.
Void main()
{
char s1[]=”Hello”;
int i=strcmp(s1,”Hello”);
cout<<i;
}
The output would be – 0
Object
Objects are the basic runtime entities in an object oriented system.
They may represent a person, place, a bank account or any item that the
program must handle.
Programming problem is analyzed in terms of objects and the nature of
communication between them.
Class
Class is a way to bind data and its associated functions together.
An object is nothing but a variable, whose data type is class.
Once a class has been defined, user can define any number of objects belonging
to that class.
A class is a collection of objects of similar type.
Eg.
Apple, Mango & Orange are objects of the class fruit.
Data Abstraction
Abstraction refers to the act of representing essential features.
Classes use the concept of abstraction and are defined as a list of abstract
attributes.
The attributes are sometimes called data member because they hold information.
Data Encapsulation / Hiding
The wrapping up of data and function into a single unit is known as
encapsulation
The functions which are wrapped in the class can access the data.
This insulation of the data by the program is called data hiding.
Inheritance
Inheritance is the process by which objects of one class acquire the properties of
objects of another class.
It also stands for reusability. This means that the additional features can be
added to an existing class without modifying it.
The mechanism of deriving a new class from an existing one is called as
inheritance.
Polymorphism
It means the ability to take more than one form.
Eg.
When you add two integers you will get new integer(by adding) but when you
add two strings you will get new string by concatenation (joining).
class classname
{
private:
Declaration of data members;
Declaration of member functions;
pubic:
Declaration of data members;
Declaration of member functions;
};
The members declared as private can be accessed only within the class and public can
be accessed from outside the class also.
Eg.
class item
{
private:
int number;
float cost;
public:
void getdata(int a,int b);
void putdata();
};
Q12. Describe how member function of class can be defined.
Ans-
Member functions of class can be defined and accessed in two ways-
Outside the class
Inside the class
Outside the class
The general form of accessing the data from outside the class definition is
classname :: functionname
Where the classname indicates the name of class and “::” is scope reolution
operator, specifies that the scope of the function is restricted.
Syntax
Eg.
class circle
{
public:
int r;
void area();
};
void circle :: area()
{
float c=2*3.14*r;
cout<<c;
}
Inside the class
Another method for defining a member function is to replace the function
declaration by the actual function definition.
Eg.
class circle
{
public:
int r;
void area()
{
float c=2*3.14*r;
cout<<c;
}
};
Q13. What is object? Describe how members of a class can be accessed using object of
that class?
Ans-
An object is a variable whose datatype is class.
User can have more than one object for a class. The object can be declared as :
class_name object1,object 2
Accessing members of class using object
The private data of a class can be accessed only through the member functions
of that class.
To use a member function, the dor operator (.) connects the object name & the
member function.
The dot operator is called as class member access operator.
Syntax
objectname.functionname(actual argument)
Eg.
class student
{
public:
void display()
{
cout<<”Hello”;
}
};
void main()
{
student o;
o.display();
}
Eg.
#include<iostream.h>
int a=2,b=3;
class abc
{
public:
int a,b;
int operator +(void); //overloading binary +
}
int abc :: operator + (void) //defined operator overloading
{
return (a+b);
}
void main()
{
abc x;
+x; //invokes operator +()
}
. Membership operator
.* Pointer to member operator
:: Scope resolution operator
?: Conditional Operator
= Assignment Operator
() Function call operator
[] Subscripting Operator
-> Class member access operator
?: Conditional Operator
Polymorphism
Eg.
Class B : public A
There are 5 types of inheritance-
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Single Inheritance
When a subclass inherits only from one base class, it is known as single
inheritance
A Base class
B Derived class
Multiple Inheritance
When a derived class inherits from multiple base class, it is known as multiple
inheritance.
A B
Base class Base class
C Derived class
Multilevel Inheritance
When a derived class inherits from a class that itself inherits from another class,
it is known as multilevel inheritance.
A Base class
B
Derived class of A & Base class of C
C Derived class of B
Hierarchical Inheritance
When a derived class inherits only from single base class, it is known as
hierarchical inheritance.
B C D
A
Hierarchical
B C
D Multiple
Input stream
Read data data input
Output stream
Ans-
ifstream
It provides input operations
It is used to read a stream of objects from a file
It contains opne() function with default input mode.
It inherits get(), getline(), read(), seekg) and tellg() functions from istream.
ofstream
It provides output operations.
It is used to write a stream of objects in a file.
It contains open() function with default output mode.
It inherits put(), seekp(), tellp() and write() function from ostream.
filebuf
its purpose os to set the file buffers to read and write.
It contains close() and open() as member.
fstream
It provides support for simultaneous input and output operations.
It contains open() with default input mode.
It inherits all the functions from istream and ostream classes through iostream.
Ans-
open()
This method is used to open a file.
It takes only one argument nad that is file name
Syntax
File-stream-class stream-object;
Stream-object.open(“file-name’);
Eg.
ofstream my;
my.open(“Try”);
close()
This method is used to close the file, which is opened for read, write or read and
write operations.
Eg.
my.close();
seekg()
This function is used to move the file pointer forwards with given number of
bytes.
Syntax
seekg(unsigned int);
Eg.
ifstream my(“xyz.dat”);
my.seekg(10);
In above example, seekg() moves input pointer 10 bytes forward.
Seekg is associated with input pointer or get pointer.
seekp()
This function is used to reposition file pointer to a given number of bytes.
This function is associated with output pointer.
Syntax
Eg
ofstream my;
my.seekp(10);
In above example, output pointer will point 10th byte in the file.
tellg()
This function is used to return current file pointer position,
This function is associated with input file strem.
Eg.
ifstream my;
my.open(“xyz.dat”);
int n;
n=my.tellg();
In above example, tellg() will return value zero, because initially, input pointer
points to zeroth location.
tellp()
This function is used to return current file pointer (output pointer) position.
It is associated with output file stream.
Eg.
ofstream my;
my.open(“xyz”,ios::app);
int n=my.tellp();
In above example file is opened in append mode, hence file pointer points to
end-of-file character. Tellp() returns number of characters present in file xyz.
put()
This function is sued to store a single character inti filw, specified by object of
ofstream.
Syntax
Ofstream object.put(character variable);
Eg.
ofstream my;
my.open(“xyz”);
char x=’a’;
my.put(x);
get()
This function is used to read a character from a file specified by ifstream object.
Syntax
Eg.
char x;
ifstream my(“xyz”);
while(my.eof()==0)
{
my.get(x);
cout<<x;
}