Introduction to C++ and differences between C
Learning Objective
- To know differences between C and C++
- Demonstrate the capability of programming in C++
- Apply C++ syntax, New/Delete and Reference operations
Resources
Problem Sets
Complete the following problem sets with in 2 hoursProblem Set A
1. Write a simple ‘Hello, new world!’ program in C and CPP edited, compiled and running on your chosen development platform. Change the program to take a command line argument (or multiple command line arguments) and to say ‘Hello name!’2. Tabulate all the basic C/C++ types, eg. Char, int, float, double. Don’t forget the unsigned, signed, short, long etc. Write a simple C and C++ program to print out the sizes in bytes of each type on your system.
3. Find out on your system what the largest and smallest values are for the basic types above? Write a program in C and C++ to print out the Limits of each data type described in previous program.
Problem Set B
1. Write a function to print the date, for the structure Date in C and C++.You need to write the code wherever necessary for the given program in C and C++.
Program to print date in C
#include//Standard Input and Output Library struct DATE{ int day; int month; int year; }; void showDate(struct DATE d); void setDate(int d, int m, int year, struct DATE * date); int main(void){ struct DATE date; setDate(24,8,2009,&date); showDate(date); return 1; } void showDate(struct DATE d){ //Write your code here } void setDate(int d, int m, int y, struct DATE * date){ //Write your code here }
#include//Input/Output stream Library using namespace std; struct Date{ int day; int month; int year; void showDate(){ //Write your code here } void setDate(int d, int m, int y){ //Write your code here } }; int main(void){ struct Date date; date.setDate(24,8,2009); date.showDate(); system("PAUSE"); return 1; }
2. Write the struct Date(of previous program) in separate namespace in C++ and use the Date structure in main to print the date.
Problem Set C
Write the code in C++ for all the problems given in this problem set.1. STDIO: Read an integer value N from standard input.
- Print “Hai” for N times using a for-loop.
- http://www.cplusplus.com/doc/tutorial/program_structure.html
- http://www.cplusplus.com/doc/tutorial/basic_io.html
- http://www.cplusplus.com/doc/tutorial/control.html
- Read your firstName
- Read your lastName
- Use “+” string operator and contenate firstName and lastName with “_” in between.
- Display firstName_lastName on the screen
- Use length() function of string class and display the length of “firstName_lastName”
- Access the characters in the string “firstName_lastName” and display one by one separated by a single space
- Ex: Display “Kishore_Prahallad” as “K i s h o r e _ P r a h a l l a d”
- Display the characters in reverse order
- Display as “d a l l a h a r P _ e r o h s i K”
- http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html
- http://www.cppreference.com/wiki/string/start
- New and Delete for integer:
- Read an integer N
- Create a 1-dimensional dynamic integer array using new[] for N integers
- Read N integers from the standard input
- Delete the allocated memory using delete[]
- New and Delete for Strings:
- Read an integer N
- Create a 1-dimensional dynamic string array using new[] for N strings
- Read N strings from the standard input
- Delete the allocated memory using delete[]
- New and Delete for 2-dimensional Matrix:
- Read two integers N and M
- Create a 2-dimensional dynamic integer array using new[]
- Read N x M integers from standard input
- Delete using delete[]
Resources:
Very good post. These are real evergreen articles.
ReplyDeleteYou can use this trick in Blogger to do Syntax Highlighting - http://thecustomizewindows.com/2012/07/syntax-highlighting-in-wordpress-without-plugin/
Thanks abhishek for your support
Delete