C++ Programming Code Examples
C++ > Beginners Lab Assignments Code Examples
Complete implementation of Publication Class
/* Complete implementation of Publication Class */
# include<iostream.h>
# include<conio.h>
class publication
{
char title[30];
float price;
public:
void getdata()
{
cout<<"Enter the title :
"; cin>>title;
cout<<"Enter the price :
"; cin>>price;
}
void putdata()
{
cout<<"The title is : "<<title<<endl;
cout<<"Price is : "<<price<<endl;
}
};
class sales
{
float s1,s2,s3; // getting the sale of last three momths
public:
void getdata()
{
cout<<"Enter the sale of first month:
";
cin>>s1;
cout<<"Enter the sale of second month:
";
cin>>s2;
cout<<"Enter the sale of third month:
";
cin>>s3;
}
void putdata()
{
cout<<"Sale in first month : $"<<s1<<endl;
cout<<"Sale in sec. month : $"<<s2<<endl;
cout<<"Sale in third month : $"<<s3<<endl;
}
};
class book: private publication, private sales
{
int pages;
public:
void getdata()
{
cout<<" BOOK DETAILS
";
publication::getdata();
sales::getdata();
cout<<"Enter the number of pages
";
cin>>pages;
}
void putdata()
{
cout<<" BOOK DETAILS
";
publication::putdata();
cout<<"Number of pages : " <<pages<<endl;
sales::putdata();
}
};
class tape: private publication,private sales
{
float time;
public:
void getdata()
{
cout<<" TAPE DETAILS
";
publication::getdata();
sales::getdata();
cout<<"Enter the time length of the casette"; cin>>time;
}
void putdata()
{
cout<<" TAPE DETAILS ";
publication::putdata();
cout<<"Time length :"<< time<<endl;
sales::putdata();
}
};
void main()
{
book b;
tape t;
b.getdata();
t.getdata();
b.putdata();
t.putdata();
}
The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.
A predefined object of the class called iostream class is used to insert the new line characters while flushing the stream is called endl in C++. This endl is similar to \n which performs the functionality of inserting new line characters but it does not flush the stream whereas endl does the job of inserting the new line characters while flushing the stream. Hence the statement cout<<endl; will be equal to the statement cout<< '\n' << flush; meaning the new line character used along with flush explicitly becomes equivalent to the endl statement in C++.
The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.
In C++, constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C++ has the same name as class or structure. Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object. Whereas, Destructor on the other hand is used to destroy the class object. • Default Constructor: A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C++ programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number. C++ array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations.
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
Any programming language has built in data types. Data types are used to create variables or your new data types. Variable is a amount of memory that has its own name and value.
To find 'cube root of any number' we need to find 0.3 power of any number. if you need to find cube root of 27 then calculate 0.3 power of 27, result is 3. So another method for this
The Fibonacci series is infinite but we can not make a C++ program that display an "infinite" output. In program, we take input the "range" in integer upto which Fibonacci series will be
To reverse an array in C++ programming, you have to ask to the user to enter the array size and array elements. Now start swapping the array elements. Make a variable say temp of