Friday, 8 April 2016

CLASS AND OBJECT
CLASS:-           CLASS IS ABSTRACT USER DEFINED DATA TYPE
                                   CLASS IS WAY OF BINDING DATA AND METHOD TOGETHER
                                   CLASS IS GROUP OF SIMILAR KIND OF OBJECTS
Defining a class in c++
Student
Roll
Name
Age
Marks

Void get() // to accept student details
Void show()// to display student details

If we have to define class in C++ for the above class represented in diagram we can define like this
#include<iostream.h>
#include<conio.h>
class student
{
int roll,age,marks;       // by default data are private, private data are not accessible out side class
char name[20];
public:                     // functions are public means it will be accessible outside the class using object of class
void get()
{
cout<<"\n enter roll";
cin>>roll;
cout<<"\n enter name";
cin>>name;
cout<<"\n enter age";
cin>>age;
cout<<"\n enter roll";
cin>>marks;
}
void show()
{
cout<<"Name:"<<name<<endl;
cout<<"Noll:"<<roll<<endl;
cout<<"Age:"<<age<<endl;
cout<<"Marks:"<<marks<<endl;
}
};
void main()
{
student s;
s.get();
s.show();
getch();
}
Here function is defined inside the class. We can also define function outside the class as give below
#include<iostream.h>
#include<conio.h>
class student
{
int roll,age,marks;                   // by default data are private, private data are not accessible outside class
char name[20];
public:                           // functions are public means it will be accessible outside the class using object of class
void get(); // function prototype
void show();// function prototype
};
void student::get() // function is define using scope resolution operator , student::get() mean get of student class
{
cout<<"\n enter roll";
cin>>roll;
cout<<"\n enter name";
cin>>name;
cout<<"\n enter age";
cin>>age;
cout<<"\n enter roll";
cin>>marks;
}
void student::show()               // function is defined  using scope resolution operator , student::show() mean show                                                    of student class
{
cout<<"Name:"<<name<<endl;
cout<<"Noll:"<<roll<<endl;
cout<<"Age:"<<age<<endl;
cout<<"Marks:"<<marks<<endl;
}
void main()
{
student s;
s.get();
s.show();
getch();
Access modifiers :- (or access specifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members.
1.    Public :- accessible every where
2.    Protected :- accessible inside the class and in derived class
3.    Private:- accessible only by member function of class.

Using class in main Program
Class can be used in main program by declaring object
Since private members are not accessible outside object can access only public member
As shown in given example
void main()
{
student s;          // s is object of class student
s.get();                    // accessing get function(using .dot operator) of student class as it is public
s.show();                // accessing show function(using .dot operator) of student class as it is public
getch();
}
Array of object
Student s can store details of one student. If we have to store details of more than one student we can create array of student object like
Student s[5];
We can call the function student[i].get()
Example
void main()
{
student s[5];          //five  object of class student
for(i=0;i<5;i++)
{
S[i].get();              
}
for(i=0;i<5;i++)
{
S[i].show();          
}
getch();

}

}

Monday, 1 February 2016

practice question for class & object

Class & Object 4 marks
1.       Define a class practical _record with following details
Private member
Experiment_no of integer type to store experiment number
Title   of string type to store title of the experiment
Guide_by   of string type to store the name of teacher  who guide for practical
Grade   of character type store the grade ‘A’, ‘B’ ,‘C’,’D’,’E’,’F’ calculated by function assign_grade() function
Marks of integer type to store the marks awarded for the experiment out of 10
Void Assign_grade()
To assign grade as per following details
Marks                                                                   grade
>9                                                                                A
>8                                                                                B
>7                                                                                C
>6                                                                                D
>5                                                                                E
<5                                                                                F                        
Public member
Void Get_details() / / to take input for Experiment_no, Title,Guide_by,Marks
Void Show_details()// to show all details of experiment and also call the function Assign grade

2.       Define a class folder with following details
Private members
Name of string type to store name of folder
Total_Size of type long to store the memory occupied by the folder
Owner  of type string to store the name of user who create the folder
No_of _files of integer type to store number of file stored in folder
Size to store the required size of new file
Void Add_files()   ask user to input size of file  and calculate the  Total_size=Total_size+size And No_of_ Files=No_of_files+1
Public members
A constructor to initialize Total_size=0 No_of_files=0
Void get() to take input of Name, owner and call the function Add_files()
Void show files to show all details of folder class
3.       Define a class Cricket with following details
Team1 of string type to store the name of country from which team1 belongs
Team2  of string type to store the name of country from which team2 belongs
Captain1 of string type to store the name of  to store the name of captain of team1
Captain2 of string type to store the name of  to store the name of captain of team2
Score1,score2 of type integer to score the run scored by team1,and team2
Winner of string type to store the name of winning team country
Void select_winner() to store  the name of winning team country in winner  by comparing the score of team1 and team2
Public member
Constructor to initialize Team1 as India and Team2  as Australia
Void get() to take input captain1, captain2,score1 and score2
Void show to display team1 , team2, captain1, captain2,score1 and score2 and call the function select_winner()


4.        Define a class Income_tax with following details
Private members
Total_income to store the total income
Name of string type to store the name of employee
Savings of type float to store the total savings done by employee
Tax_income to store the taxable_income
Gender of type character to store “M” for male and “F” for female
Tax of float type to store tax to be paid by employee
Void Calc_tax() to calculate tax_income and tax as 10% of tax_income
Savings can be done maximum 1,50,000 if saving is more than 1,50,000 it will be equal to 1,50,000 only
For female Tax_income is Total_income_2,50,000-savings
For male Tax_income is Total_income_2,00,000-savings
Tax=10% of tax_income
Public member
Void get() to take input of Total_income,Savings, Name and gender as “M” or “F” and call the function void Calc_tax()
Void show() to show all details

5.       Define a class student with following details
Name of string type to store name of student
Age of float type to store age of students
Marks to store marks obtained by students out of 100
Grade of character type o store A,B,C,D,E,OR F grade
Void Calc_grade() to set the value of grade according to marks
Marks                   grade
A                             >90        
B                             >80                        
C                             >70
D                             <70
PUBLIC MEMBER
Void get() to take input of all required details and also call the function Calc_grade()

Void show () to show all details of student class

Friday, 29 January 2016

2 Marks important question from Revision Tour


1)      What is local and global variable?
2)      What is automatic data type conversion?
3)      What is default parameter?
4)      What is call by value and call by reference?
5)      What is enumerated data type?
6)      What is typedef? Explain with example?
7)      What is use of header files?
8)      What is pre-processor? Explain with example?
9)      What is function overloading?
10)   What is difference between switch and if statement?
11)   What is function? What are the benefits of using function?
12)   What is random function?
13)   What is syntax error?
14)   What is runtime error?
15)   What is logical error?
16)   What is difference between break and return?

17)  What is the difference between nesting or containership and inheritance? Explain with example? 
18) what is random() and randomize function
19) What is actual and formal parameter ?